练习:
- 查询入职日期比10部门任意一个员工晚的员工姓名、入职日期,不包括10部门员工
select e.ename,e.hiredate from emp e where e.hiredate>any (select hiredate from emp where deptno =10) and e.deptno<>10;
- 查询入职日期比10部门所有员工晚的员工姓名、入职日期,不包括10部门员工
select e.ename,e.hiredate from emp e where e.hiredate>all (select hiredate from emp where deptno =10) and e.deptno<>10;
- 查询职位和10部门任意一个员工职位相同的员工姓名,职位,不包括10部门员工
select e.ename,e.job from emp e where e.job=any (select job from emp where deptno =10) and e.deptno<>10;
- 查询部门平均工资在2500元以上的部门名称及平均工资。
select d.dname,avg(e.sal) from emp e,dept d group by e.deptno,d.dname having avg(e.sal)>2500;
- 查询员工岗位中不是以“SA”开头并且平均工资在2500元以上的岗位及平均工资,并按平均工资降序排序。
select e.job,avg(e.sal) from emp e,dept d where e.deptno=d.deptno and e.job not like 'SA%' group by e.job having avg(e.sal)>2500 order by avg(e.sal);
- 查询部门人数在2人以上的部门名称、最低工资、最高工资。
select d.dname,min(e.sal),max(e.sal) from emp e,dept d where e.deptno =d.deptno group by d.dname having count(*)>2;
- 查询岗位不为SALESMAN,工资和大于等于2500的岗位及每种岗位的工资和。
select e.job,sum(e.sal) from emp e where e.job <> 'SALESMAN'group by e.job,e.sal having sum(e.sal)>2500;
- 显示经理号码和经理姓名,这个经理所管理员工的最低工资,没有经理的KING也要显示,不包括最低工资小于3000的,按最低工资由高到低排序
select e.empno 经理编号,e.ename 经理姓名,min(w.sal) 员工最低工资 from emp w left join emp e on (w.mgr=e.empno) group by e.empno,e.ename having min(w.sal)>=3000 order by min(w.sal)desc;
- 查询工资高于编号为7782的员工工资,并且和7369号员工从事相同工作的员工的编号、姓名及工资。
select e.empno,e.ename,e.sal from emp e where e.sal=(select min(sal) from emp);
select e.ename,e.sal from emp e where e.sal=(select max(sal) from emp);
- 查询部门最低工资高于10号部门最低工资的部门的编号、名称及部门最低工
资。
select e.deptno,d.dname,min(e.sal) from emp e,dept d where e.deptno=d.deptno group by e.deptno having min(e.sal)>(select min(sal) from emp where empno=10);
- 查询员工工资为其部门最低工资的员工的编号和姓名及工资。
select e.empno,e.ename,e.sal from emp e where e.sal=(select min(sal) from emp);
select e.ename,e.sal from emp w left join emp e on(e.mgr=w.empno) where e.mgr=(select empno from emp where ename='KING');
- 显示比员工SMITH参加工作时间晚的员工姓名,工资,参加工作时间。
select e.ename,e.sal,e.hiredate from emp e where e.hiredate>(select hiredate from emp where ename='SMITH');