SQL語句複習【專題五】sql
單行子查詢:只會獲得一個結果的子查詢【子查詢的內容必須放在小括號中。在查詢語句中的查詢語句 】
--查詢全部比 CLARK 員工 工資高的員工
--1.先查詢 CLARK 員工的工資
select sal from emp where ename='CLARK'--2450
--2.而後拿着CLARK的工資去比較
select * from emp where sal > 2450 order by sal
--3.合二爲一 => 子查詢
select * from emp where sal > (select sal from emp where ename='CLARK') order by sal
ide
--查詢工資高於平均工資的員工的名字和工資
--1.先查詢平均工資
select avg(sal) from emp
--2.合二爲一 => 子查詢
select ename, sal from emp where sal > (select avg(sal) from emp)spa
--查詢和scott同一個部門的,可是比scott入職時間早的員工的信息
--1.先查詢 scott 的部門編號
select deptno from emp where ename='SCOTT'--20
--2.查詢scott 的入職時間
select hiredate from emp where ename='SCOTT'--87,4,19
--3.最終SQL語句
select * from emp where deptno=(select deptno from emp where ename='SCOTT') and hiredate<(select hiredate from emp where ename='SCOTT')code
--查詢 和 scott 同一個部門,可是比scott工資低的員工的信息
--1.先查詢 scott 的部門編號
select deptno from emp where ename='SCOTT'--20
--2.在查詢scott 的工資
select sal from emp where ename='SCOTT'--3000
--3.最終SQL語句
select * from emp where deptno=(select deptno from emp where ename='SCOTT') and sal < (select sal from emp where ename='SCOTT')blog
--查詢 比scott 工資高或者入職時間比scott早的員工的編號 和姓名
--1.查詢scott 的工資
select sal from emp where ename='SCOTT'--3000
--3.查詢scott 的入職時間
select hiredate from emp where ename='SCOTT'--87,4,19
--3.最終SQL語句
select empno, ename from emp
where sal > (select sal from emp where ename='SCOTT') or hiredate< (select hiredate from emp where ename='SCOTT')排序
多行子查詢:子查詢的結果會有多個
1) all: 和全部的子查詢的結果去比較
2) any:和子查詢的結果集中的任意一個比較
3) in: 和子查詢的結果集中的某一個比較是否相等event
--查詢工資低於任意一個 ‘CLERK’的工資的員工信息
--1.先查出全部的 職員 的工資
select sal from emp where job='CLERK'
--2.最後SQL語句
select * from emp where sal < any(select sal from emp where job='CLERK')class
--查詢工資 比全部的'SALESMAN'都高的員工的編號,姓名,工資
--查詢全部的銷售人員的工資
select sal from emp where job='SALESMAN'
--2.最後SQL語句
select empno,ename,sal from emp where sal > all(select sal from emp where job='SALESMAN')cli
--查詢20號部門的中職務和10號部門職務相同的員工的信息
--1.先求10號部門的全部的人的工做
select distinct job from emp where deptno=10
--2.最終SQL
select * from emp where deptno=20 and job in(select distinct job from emp where deptno=10)sed
--查詢哪些員工是領導,將領導的信息所有顯式
--in any
--統計領導的編號
select distinct mgr from emp where mgr is not null
-- in 員工的編號 在領導的編號的集合中
select *from emp where empno in(select distinct mgr from emp where mgr is not null)
--any 員工的編號等於 領導的編號集合中的某一個
select * from emp where empno=any(select distinct mgr from emp where mgr is not null)
--查詢20號部門中收入最高的職員的信息
--先求20號部門的最高工資 3000
select max(sal) from emp where deptno=20
--最終SQL語句
select *from emp where sal=(select max(sal) from emp where deptno=20)
--查詢全部部門的平均薪水的等級
-- 1.先查詢全部的部門的平均薪水
select deptno,avg(sal) from emp group by deptno
--將子查詢做爲一張表
--sql 92
select T.deptno, T.avg_sal, s.grade
from (select deptno,avg(sal) avg_sal from emp group by deptno) T, salgrade s
where T.avg_sal between s.losal and s.hisal
--sql 99
select T.deptno, T.avg_sal, s.grade
from (select deptno,avg(sal) avg_sal from emp group by deptno) T join salgrade s
on T.avg_sal between s.losal and s.hisal
--查詢部門的詳細信息,以及部門的平均工資和工資等級 三表查詢
-- 查詢全部的部門的平均薪水
select deptno,avg(sal) from emp group by deptno
--sql92
select d.*,T.avg_sal,s.grade
from (select deptno,avg(sal) avg_sal from emp group by deptno) T, salgrade s,dept d
where T.avg_sal between s.losal and s.hisal and T.deptno=d.deptno
--sql99
select d.*,T.avg_sal,s.grade
from (select deptno,avg(sal) avg_sal from emp group by deptno) T join salgrade s
on T.avg_sal between s.losal and s.hisal
join dept d
on T.deptno=d.deptno
相關子查詢
--1:查詢全部的部門的最高工資的員工的信息
--查詢10部門的最高工資的員工的信息
--10部門的最高工資
select max(sal) from emp where deptno=40
select * from emp where deptno=10 and sal=(select max(sal) from emp where deptno=10)--1
select * from emp where deptno=20 and sal=(select max(sal) from emp where deptno=20)--2
select * from emp where deptno=30 and sal=(select max(sal) from emp where deptno=30)--1
select * from emp where deptno=40 and sal=(select max(sal) from emp where deptno=40)
-----------------------------------------------------------------------------------------------------------------------------
select * from emp e where sal=(select max(sal) from emp where deptno=e.deptno)
--2:查詢工資高於其所在部門的平均工資的員工的信息
--查詢工資高於10部門的平均工資的10部門員工的信息
--10部門的平均工資
select avg(sal) from emp where deptno=10
select * from emp where deptno=10 and sal > (select avg(sal) from emp where deptno=10)--1
select * from emp where deptno=20 and sal > (select avg(sal) from emp where deptno=20)--3
select * from emp where deptno=30 and sal > (select avg(sal) from emp where deptno=30)--2
-------------------------------------------------------------------------------------------------------------------------------------
select * from emp e1 where sal > (select avg(sal) from emp e2 where e2.deptno=e1.deptno)
總結:
--不相關子查詢:能夠單獨運行,先執行子查詢再執行 外查詢。
--相關子查詢:不能夠單獨運行,先執行外查詢,再執行子查詢。
--3:查詢全部部門的最高工資的員工的信息
--查詢全部部門的最高工資
select deptno, max(sal) from emp group by deptno
--方式-1
select e.*
from emp e, (select deptno, max(sal) max_sal from emp group by deptno) T
where e.deptno=T.deptno and e.sal=T.max_sal
--方式-2
select * from emp where (deptno,sal) in (select deptno, max(sal) from emp group by deptno)
----------------------------------------------------------------小練習---------------------------------------------------------------------
1 --一、列出全部員工的年工資,按年薪從低到高排序。 2 select ename, sal*12+nvl(comm,0)*12 year_sal from emp order by year_sal 3 --二、列出薪金比「 SMITH 」多的全部員工。 4 select * from emp where sal>(select sal from emp where ename='SMITH') 5 --三、列出全部員工的姓名及其直接上級的姓名。 sql92 , sql99 6 --sql92 7 select e1.ename,e2.ename 8 from emp e1, emp e2 9 where e1.mgr= e2.empno 10 --sql99 11 select e1.ename,e2.ename 12 from emp e1 join emp e2 13 on e1.mgr= e2.empno 14 --四、列出受僱日期早於其直接上級的全部員工。 15 --sql99 16 select e1.* 17 from emp e1 join emp e2 18 on e1.mgr=e2.empno 19 where e1.hiredate < e2.hiredate 20 --五、列出部門名稱和這些部門的員工信息,包括那些沒有員工的部門。 21 select d.dname,e.* 22 from emp e right join dept d 23 on e.deptno=d.deptno 24 --六、列出全部job 爲「 CLERK 」(辦事員)的姓名及其部門名稱。 25 select e.ename,d.dname 26 from emp e join dept d 27 Using(deptno) 28 where e.job='CLERK' 29 --七、列出最低薪金大於1500 的各類工做。 30 select job, min(sal) 31 from emp 32 group by job 33 having min(sal) > 1500 34 --八、列出在部門「 SALES 」(銷售部)工做的員工的姓名,假定不知道銷售部的部門編號。 35 select e.deptno,e.ename 36 from emp e join dept d 37 on e.deptno=d.deptno 38 where d.dname='SALES' 39 --九、列出薪金高於公司平均薪金的全部員工。 40 select * from emp where sal > (select avg(sal) from emp) 41 --十、列出與「 SCOTT 」從事相同工做的全部員工。 42 select * from emp where job=(select job from emp where ename='SCOTT') and ename<>'SCOTT' 43 --十一、列出薪金高於在部門 30 工做的全部員工的薪金的員工姓名和薪金。 44 select ename,sal from emp where sal>all(select sal from emp where deptno=30) 45 --十二、列出在每一個部門工做的員工數量、平均工資和平均服務期限(年)。 46 select deptno, count(*), avg(sal) , avg(to_char(sysdate,'YYYY')-to_char(hiredate,'YYYY')) 平均服務年限 47 from emp 48 group by deptno 49 --1三、列出全部員工的姓名、部門名稱和工資。 50 select e.ename,d.dname,e.sal from emp e natural join dept d 51 --1四、列出從事同一種工做但屬於不一樣部門的員工的一種組合。 52 select e1.ename,e1.job ,e1.deptno,e2.ename,e2.job,e2.deptno 53 from emp e1 join emp e2 54 on e1.job=e2.job and e1.deptno<>e2.deptno and e1.ename >e2.ename 55 --1五、列出全部部門的詳細信息和部門人數。 56 select d.*, count(*) 57 from emp e join dept d 58 on e.deptno=d.deptno 59 group by d.deptno,d.dname,d.loc 60 61 select d.*,count(e.ename) from dept d left join emp e 62 on d.deptno = e.deptno 63 group by d.deptno,d.dname,d.loc 64 --1六、列出各類工做的最低工資。 65 select job,min(sal) from emp group by job 66 -- 17 、列出各個部門的 MANAGER (經理)的最低薪金。 67 select deptno,min(sal) 68 from emp 69 where job='MANAGER' 70 group by deptno 71 --1八、列出至少有一個員工的全部部門。 72 select d.*,count(*) 73 from emp e join dept d 74 on e.deptno=d.deptno 75 group by d.deptno,d.dname,d.loc 76 having count(*)>0