1 列出至少有一个员工的所有部门
select d.deptno,d.dname
from dept d,( select d.deptno
from emp e,dept d
where e.deptno(+)=d.deptno
group by d.deptno
having count(e.empno)>1 ) t
where d.deptno=t.deptno
2 列出薪水比'SMITH'多的所有员工
select *
from emp e
where e.sal>(select sal
from emp
where ename='SMITH') and e.ename!='SMITH'
3 列出所有员工姓名和直接上级的姓名
select e.ename,m.ename
from emp e,emp m
where e.mgr=m.empno(+)
4 列出受雇日期早于其直接上级的所有员工的编号,姓名和部门名称
select distinct e.empno,e.ename,d.dname
from emp e,emp m,dept d
where e.mgr=m.empno and e.hiredate<m.hiredate and e.deptno=d.deptno
5 列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门
select e.*,d.dname
from emp e,dept d
where e.deptno(+)=d.deptno
6 列出所有'CLERK'的姓名和部门名称,部门人数
select e.ename,d.dname,t.cout
from emp e , dept d,(select d.deptno,count(empno) cout
from emp e ,dept d
where e.deptno(+)=d.deptno
group by d.deptno ) t
where e.job='CLERK' and e.deptno=d.deptno and d.deptno=t.deptno
7 列出最低薪金大于1500的各种工作及从事此工作的全部雇员人数
select job,nvl(count(empno),0)
from emp
group by job
having min(sal)>1500
8 列出在部门'SALES'工作的员工姓名,假定不知道销售部门的部门编号
select e.ename
from emp e
where e.deptno in ( select deptno
from dept
where dname='SALES' )
9列出薪金高于公司平均薪金的所有员工,所在部门,上级领导,工资等级。
select e.empno,e.ename,d.dname,m.ename,s.grade
from emp e,emp m,salgrade s ,dept d
where e.mgr=m.empno(+) and e.sal between s.losal and s.hisal and e.deptno=d.deptno
and e.sal > (select avg(sal)
from emp )
10 列出与'SCOTT '从事相同工作的所有员工及部门名称
select e.empno,e.ename,d.dname
from emp e,dept d
where job in (select distinct job
from emp
where ename='SCOTT') and e.deptno=d.deptno and e.ename!='SCOTT'
11 列出薪金等于部门30中员工的薪金的所有员工的姓名和薪金
select e.ename,sal
from emp e
where e.deptno!='30' and e.sal in ( select distinct sal
from emp
where deptno='30' )
12 列出薪金高于部门30中的所有员工的薪金的所有员工的姓名和薪金,部门名称
select ename,sal,dname
from emp e,dept d
where sal > (select max(sal)
from emp
where deptno='30') and e.deptno=d.deptno and d.deptno!='30'
13 列出每个部门工作的员工数量,平均工资和平均服务年限
select d.dname , nvl(avg(sal),0),
nvl(avg(months_between(sysdate,hiredate))/12,0),nvl(count(empno),0)
from emp e,dept d
where e.deptno(+)=d.deptno
group by d.dname
14 列出所有员工的姓名,部门名称和工资
select e.ename,d.dname,sal
from emp e,dept d
where e.deptno=d.deptno
15 列出所有部门的详细信息和部门人数
select d.*,t.cout
from dept d,(select d.deptno,nvl(count(empno),0) cout
from emp e,dept d
where e.deptno(+)=d.deptno
group by d.deptno ) t
where d.deptno=t.deptno
16 列出各种工作最低工资及从事此工作的雇员姓名
select e.job,e.ename,e.sal
from emp e , ( select job ,min(sal) minsal
from emp
group by job ) t
where e.job=t.job and e.sal =t.minsal
17 列出各个部门的MANAGER的最低薪金
select deptno,min(sal)
from emp
where job='MANAGER'
group by deptno
18 列出所有员工的年工资,按年薪从低到高排序
select empno,ename, 12*(sal+nvl(comm,0)) yearsal
from emp
order by yearsal asc
19 查出某个员工的上级主管,并要求这些主管的薪水超过3000
select distinct m.empno,m.ename
from emp e,emp m
where e.mgr=m.empno(+) and m.sal>3000
20 求出部门名称中带'S'字符的部门员工的工资合计,部门人数
select d.dname,t.sumsal,t.cout
from dept d,(select d.deptno,nvl(sum(sal),0) sumsal ,nvl(count(empno),0) cout
from emp e ,dept d
where e.deptno(+)=d.deptno and d.dname like '%S%'
group by d.deptno ) t
where d.deptno=t.deptno
21 给任职日期超过10年的人加薪10%
update emp
set sal=sal+(sal*0.1)
where months_between(sysdate,hiredate)/12>0