emp员工表(empno员工号/ename员工姓名/job工作/mgr上级编号/hiredate受雇日期/sal薪金/comm佣金/deptno部门编号)
dept部门表(deptno部门编号/dname部门名称/loc地点)
说明:
1.列出至少有一个员工的所有部门
select deptno,dname from dept where deptno in (select deptno from emp group by deptno having count(*)>0);
select * from emp where sal>(select sal from emp where ename='SMITH');
3.列出所有员工的姓名及其直接上级的姓名。
select e1.ename '员工', e2.ename '上级' from emp e1 left join emp e2 on e1.mgr=e2.empno;
select ename,(select ename from emp where empno=a.mgr) from emp a;
连接查询:
select e1.ename,e1.hiredate,e2.hiredate
from emp e1 left join emp e2 on e1.mgr=e2.empno
where e1.hiredate>e2.hiredate;
子查询:
select ename from emp a
where hiredate>(select hiredate from emp where empno=a.mgr);
select d1.dname,e1.ename from emp e1 right join dept d1 on e1.deptno=d1.deptno order by d1.dname;
select ename,(select dname from dept where deptno=e1.deptno) deptname from emp e1 where job='CLERK';
select dname,ename from dept a,emp b where a.deptno=b.deptno and job='CLERK';
select job,min(sal) minSal from emp group by job having min(sal)>1500;
select ename from emp where deptno =(select deptno from dept where dname='SALES');
select ename from emp where sal>(select avg(sal) from emp);
select ename from emp where job=(select job from emp where ename='SCOTT');
select ename,sal from emp where sal in(select sal from emp where deptno=3);
select ename,sal from emp where sal = any(select sal from emp where deptno=3);
select ename,sal from emp where sal>all(select sal from emp where deptno=3);
select ename,sal from emp where sal>(select max(sal) from emp where deptno=3);
--下面是MySql下sql语句
--DATEDIFF()函数用来计算天数、TIMESTAMPDIFF()函数最后会介绍
select deptno,count(empno),avg(sal) avgSal,avg(DATEDIFF(now(),hiredate)) avgday from emp group by deptno;
select deptno,count(empno),avg(sal) avgSal,avg(TIMESTAMPDIFF(DAY,hiredate,now())) avgday from emp group by deptno;
--下面是Oracle下sql语句
select deptno,count(*),trunc(avg(sal)) avgsal,round(avg(MONTHS_BETWEEN(sysdate,hiredate)),2) avgday from emp group by deptno;
14.列出所有员工的姓名、部门名称和工资。
--下面是MySql下sql语句
select e.ename,d.dname,(e.sal+IFNULL(e.comm,0)) sal from emp e join dept d on e.deptno=d.deptno;
--下面是Oracle下sql语句<
select ename,dname,sal+nvl(comm,0) from emp,dept where emp.deptno=dept.deptno;
select d.*, count(empno) from emp e right join dept d on e.deptno=d.deptno group by deptno;
select a.*,(select count(*) from emp where deptno=a.deptno) tot from dept a ;
--下面是MySql下sql语句
select job,min(sal+ifnull(comm,0)) from emp group by job;
--下面是Oracle下sql语句
select job,min(sal+nvl(comm,0)) from emp group by job;
这样写有问题
select job,min(nvl(sal+comm,0)) from emp group by job;
select min(sal) from emp where job='MANAGER';
select ename,12*(sal+ifnull(comm,0)) ansal from emp order by ansal;
TIMESTAMPDIFF(interval,datetime_expr1,datetime_expr2)
说明:
返回日期或日期时间表达式datetime_expr1 和datetime_expr2the 之间的整数差。其结果的单位由interval 参数给出。interval 的法定值同TIMESTAMPADD()函数说明中所列出的相同。
SELECT TIMESTAMPDIFF(MONTH,'2009-10-01','2009-09-01');
interval可是:
秒 SECONDS
分钟 MINUTES
小时 HOURS
天 DAYS
月 MONTHS
年 YEARS
CREATE TABLE EMP
(EMPNO int auto_increment,
ENAME VARCHAR(10),
JOB VARCHAR(9),
MGR int,
HIREDATE DATETIME,
SAL int,
COMM int,
DEPTNO int,
CONSTRAINT PK_EMP PRIMARY KEY (EMPNO)
);
CREATE TABLE DEPT
(DEPTNO int auto_increment,
DNAME VARCHAR(14),
LOC VARCHAR(13),
CONSTRAINT PK_DEPT PRIMARY KEY (DEPTNO)
);
alter table EMP add constraint FK_DEPTNO foreign key (DEPTNO) references DEPT(DEPTNO);
Insert into DEPT (DEPTNO,DNAME,LOC) values (1,'ACCOUNTING','NEW YORK');
Insert into DEPT (DEPTNO,DNAME,LOC) values (2,'RESEARCH','DALLAS');
Insert into DEPT (DEPTNO,DNAME,LOC) values (3,'SALES','CHICAGO');
Insert into DEPT (DEPTNO,DNAME,LOC) values (4,'OPERATIONS','BOSTON');
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7369,'SMITH','CLERK',7902,'1980-12-17',800,null,2);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7499,'ALLEN','SALESMAN',7698,'1981-2-20',1600,300,3);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7521,'WARD','SALESMAN',7698,'1981-2-22',1250,500,3);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7566,'JONES','MANAGER',7839,'1981-4-2',2975,null,2);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7654,'MARTIN','SALESMAN',7698,'1981-9-28',1250,1400,3);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7698,'BLAKE','MANAGER',7839,'1981-5-1',2850,null,3);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7782,'CLARK','MANAGER',7839,'1981-6-9',2450,null,1);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7788,'SCOTT','ANALYST',7566,'1981-4-19',3000,null,2);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7839,'KING','PRESIDENT',null,'1981-11-17',5000,null,1);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7844,'TURNER','SALESMAN',7698,'1981-9-8',1500,0,3);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7876,'ADAMS','CLERK',7788,'1987-5-23',1100,null,2);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7900,'JAMES','CLERK',7698,'1981-12-3',950,null,3);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7902,'FORD','ANALYST',7566,'1981-12-3',3000,null,2);
Insert into EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) values (7934,'MILLER','CLERK',7782,'1982-1-23',1300,null,1);