1. 程式人生 > >oracle高階查詢練習題

oracle高階查詢練習題

1、  列出員工表中每個部門的員工數和部門編號

Select deptno,count(*) from emp group by deptno;

補充1:列出員工表中,員工人數大於3的部門編號和員工人數

Select deptno,count(*) from emp group by deptno having count(*)>3;

補充2:列出員工表中,員工人數大於3的部門的部門編號,部門名稱和部門位置

答案1:Select d.* from dept d,(select deptno,count(*) from emp group bydeptno having count(*)>3)x where d.deptno=x.deptno;

答案2:select * from dept where deptno in (select deptno from emp group bydeptno having count(*)>3);

補充3:列出員工表中,員工人數大於3的部門的部門編號,名稱,位置和員工人數。

Select d.*,x.co from dept d,(selectdeptno,count(*) co from emp group by deptno having count(*)>3)x whered.deptno=x.deptno;(注意看聚合函式的處理方式)

2、  列出員工表中每個部門的員工數(員工數必須大於3),和部門名稱

(1).先列出員工數大於3的部門號

Select deptno,count(*) from emp group by deptno having count(*)>3;

(2)列出員工表中每個部門的員工數(員工數必須大於3),和部門名稱

Select x.co,d.dname from dept d,( Select deptno,count(*) cofrom emp group by deptno having count(*)>3)x where x.deptno=d.deptno ;

3、  找出工資比JONES多的員工

Select * from emp where sal>(select salfrom emp where ename=’JONES’);Select * from emp where sal>(select sal from emp where ename=’JONES’);

4、  列出所有員工的姓名和其上級的姓名(表的自連線)

Select e1.ename,e2.ename from emp e1,emp e2where e1.mgr=e2.empno;

5、 以職位分組,找出平均工資最高的兩種職位

(1)先以職位分組並按平均工資降序排序

Select job,avg(sal)avg_sal from emp group by job order by avg_sal desc;

(2)找到前兩位

Select x.* from (Select job,avg(sal) avg_sal from emp group by job order by avg_sal desc)x where rownum <= 2;

補充:查找出不在部門20,且比部門20中任何一個人工資都高的員工的姓名部門名稱

(1)先找出部門20的最高薪水

Select max(sal) from emp where deptno =20;

(2)再找到薪水比(1)高的且不在部門20

Select e.ename,d.dname from emp e,dept dwhere e.deptno=d.deptno and e.sal>( Select max(sal) from emp where deptno =20)and e.deptno!=20;

方法2:  select e.ename,e.sal,d.dnamefrom emp e,dept d where e.deptno=d.deptno and e.deptno<>20 ande.sal>all(select sal from emp where deptno=20);

6、 得到平均工資大於2000的工作職種

Select job,avg(sal) from emp group by job havingavg(sal)>2000;

7、 分部門得到工資大於2000的所有員工的平均工資,並且平均工資還要大於2500

Select deptno,avg(sal) from emp where sal>2000 group bydeptno having avg(sal)>2500

8、  得到每個月工資總數最少的那個部門的部門編號,部門名稱,部門位置

Select *from dept where deptno in(select x.deptno from (select deptno from emp group bydeptno order by sum(sal))x where rownum=1);

(1)  先按照工資總數升序排序

 Select deptno from emp group bydeptno order by sum(sal);   --x

(2)  找到第一位(即工資總數最少)

 Select x.* from (Select deptnofrom emp group by deptno order by sum(sal))x where rownum=1;

(3)   Select * from dept where deptno=( Select x.* from (Selectdeptno from emp group by deptno order by sum(sal))x where rownum=1);

9、  分部門得到平均工資等級為3級(等級表)的部門編號

思路:(1)獲取到每個部門的平均工資x

Select deptno,avg(sal) avg_sal from emp group by deptno;

(2)獲取到平均工資的等級。需要將x與salgrade做非等值連線。

select x.*,s.* fromx,salgrade s where x.avg_sal between s.losal and s.hisal;

(3)   將第二步中的x用(1)的語句替換  --最終結果

select x.*,s.* from (Selectdeptno,avg(sal) avg_sal from emp group by deptno)x,salgrade s where x.avg_salbetween s.losal and s.hisal and s.grade=3;

10、        查找出部門10和部門20中,工資最高第3名到第5名的員工的員工名字,部門名字,部門位置

(1)部門1020按工資

Select * from emp where deptno in(10,20) order by sal desc;

2)排名3-5

Select * from (Select rownum ro ,x.*from (Select * from emp wheredeptno in(10,20) order by sal desc)x )where ro>=3 and ro <=5;

3)員工名字,部門名字,部門

Select e.ename,d.dname,d.loc from (Select * from (Select rownum ro ,x.*from(Select * from emp where deptno in(10,20) order by sal desc)x )where ro>=3and ro <=5)e,dept d where e.deptno=d.deptno;

11、        查找出收入(工資加上獎金),下級比自己上級還高的員工編號,員工名字,員工收入

Select e1.empno,e1.ename,e1.sal+nvl(e1.comm,0)from emp e1,emp e2 where e1.mgr=e2.empno and e1.sal+nvl(e1.comm,0)>e2.sal+nvl(e2.comm,0);

12、        查找出工資等級不為4級的員工的員工名字,部門名字,部門位置

(1)工資等級

Select e.*,s.grade from emp e,salrade s where e.sal between s.losal ands.hisal;

(2)工資等級不為4

Select e1.ename,d.dname,d.loc,e1.grade from ( Select e.*,s.grade fromemp e,salgrade s where e.sal between s.losal and s.hisal)e1,dept d whered.deptno=e1.deptno and e1.grade != 4;

方法2:三表連線!!!!!!!!

Select e.ename,d.dname,d.loc,s.grade from emp e,dept d,salgrade s wheree.deptno=d.deptno and e.sal between s.losal and s.hisal and s.grade<>4;

13、        查找出職位和‘MARTIN’或者‘SMITH’一樣的員工的平均工資

Select avg(sal) from emp where job in(selectjob from emp where ename in(‘MARTIN’,’SMITH’));

14、        查找出不屬於任何部門的員工

Select * from emp where deptno is null;

15、        按照部門統計員工數,查出員工數最多的部門的第二名到第五名(列出部門名字,部門位置)

(1)統計各部門員工數,並降序排序

Select count(*), deptno from emp group by deptno order by count(*) desc;

(2)2-5

Select * from (Select rownum ro,x.* from (Select count(*), deptno fromemp group by deptno order by count(*) desc)x) where ro>=2 and ro<=5;

(3)

Select d.*,x1.* from dept d,( Select * from (Select rownum ro,x.* from(Select count(*), deptno from emp group by deptno order by count(*) desc)x) wherero>=2 and ro<=5)x1 where d.deptno=x1.deptno;

16、        查出KING所在部門的部門號、部門名稱、部門人數

(1)king所在的部門

Select deptno from emp where ename=’KING’

(2)

Select d.deptno,d.dname,count(*) from empe,dept d where e.deptno=d.deptno and d.deptno=( Select deptno from emp whereename=’KING’) group by d.deptno,d.dname;

17、        查出KING所在部門的工作年限最大的員工名字

Select ename from emp where deptno=(select deptno from empwhere ename=’KING’) and hiredate=(select min(hiredate) from emp where deptno=( selectdeptno from emp where ename=’KING’));

18、        查出工資成本最高的部門的部門號和部門名稱

(1)工資成本

Select sum(sal)+sum(nvl(comm,0)) sum_sal,deptno from emp  group by deptno order by sum_sal desc;

(2)找到第一位

Select d.*,x.sum_sal from dept d ,( Select sum(sal)+sum(nvl(comm,0))sum_sal,deptno from emp  group by deptno orderby sum_sal desc)x where x.deptno=d.deptno and rownum=1;