where子句:子查詢返回結(jié)果為單行單列俭尖,單行多列氢惋,多行單列(往往使用in洞翩,any,all)操作符
having子句:子查詢返回結(jié)果單行單列數(shù)據(jù)焰望,同時(shí)為了使用統(tǒng)計(jì)函數(shù)操作
from子句:子查詢返回結(jié)果為多行多列骚亿,可以按照一張數(shù)據(jù)表(臨時(shí)表)的形式操作
select子句:了解即可,可以通過別的子句形式完成熊赖。
rownum子句:只能執(zhí)行<=運(yùn)算来屠,不能執(zhí)行>或者between and 區(qū)間的運(yùn)算,與order by連用時(shí)震鹉,要先使用order by排序(建立個(gè)臨時(shí)表)俱笛。
范例(where):
1.查詢基本工資比ALLEN低的全部雇員信息
a.找到ALLEN的工資,返回一個(gè)單行單列的數(shù)據(jù)
b. 在where子句中里面使用子查詢
select * from emp
where sal>(select sal from emp where ename='ALLEN')
2.查詢與雇員7566從事統(tǒng)一工作且領(lǐng)導(dǎo)相同的的全部雇員信息
a.找到7566的工作和領(lǐng)導(dǎo)传趾,返回單行多列的數(shù)據(jù)
b.在where子句中里面使用子查詢
select * from emp where (job,mgr)=(select job,mgr from emp where empno=7566)
and empno<>7566
3.查詢工資與每個(gè)部門中最低工資相同的全部雇員信息
a.查出每個(gè)部門最低工資是多少迎膜,返回多行單列的數(shù)據(jù)。
b.用where子句(in)
c.如果在in中子查詢返回值有NULL浆兰,那么不會(huì)影響磕仅,但如果是not in 中子查詢有null,那么不會(huì)返回任何值簸呈,用nvl函數(shù)(nvl(mgr榕订,100))
select * from emp where sal in
(select min(sal) from emp GROUP BY deptno)
4.空數(shù)據(jù)判斷
在sql中提供了一個(gè)exists結(jié)構(gòu)用于判斷子查詢是否有數(shù)據(jù)返回,如果子查詢中有數(shù)據(jù)返回蝶棋,則exists結(jié)構(gòu)返回ture卸亮,反之返回false
范例(having)
having一定是結(jié)合group by子句一起使用的,其主要目的是分組后的信息再次過濾玩裙,而與where子句不同的是,having是在分組后段直,可以使用統(tǒng)計(jì)函數(shù)吃溅。
1.查詢每個(gè)部門平均工資最高的的部門名稱及平均工資(最高的平均工資,肯定是統(tǒng)計(jì)函數(shù)的嵌套)
a.先求出部門平均工資最高的那個(gè)平均工資鸯檬,返回的是單行單列的數(shù)據(jù)
select dname,avg(sal) from emp join dept on DEPT.DEPTNO=EMP.DEPTNO
GROUP BY dname having avg(sal) =(select max(avg(sal)) from emp GROUP BY deptno )
范例(from)
from子句的主要功能是確定數(shù)據(jù)來源决侈,那么來源都屬于數(shù)據(jù)表,只要是在from子句中出現(xiàn)的內(nèi)容一般都是多行多列的子查詢返回喧务。
1.查詢薪金比ALLEN或clark多的所有員工的編號(hào)赖歌,姓名,基本工資功茴,部門名稱庐冯,領(lǐng)導(dǎo)姓名,部門人數(shù)坎穿。
a.找出所需要的表
emp:?jiǎn)T工的編號(hào)展父,姓名返劲,基本工資
dept:部門名稱
emp:領(lǐng)導(dǎo)姓名
emp:統(tǒng)計(jì)部門人數(shù)
select emp.empno,EMP.ename,EMP.SAL,DEPT.dname,s.ENAME,temp.tt
from emp,dept,emp s,(select deptno ,count(*) as tt from emp GROUP BY deptno) temp
where emp.sal>any
(select sal from emp where ename in('ALLEN','CLARK'))
and DEPT.DEPTNO=EMP.DEPTNO
and EMP.mgr=s.EMPNO
and EMP.deptno=temp.deptno
范例(select)
1.查詢每個(gè)部門的編號(hào)蔫浆,名稱浪耘,位置,部門人數(shù)运挫,平均工資
select dept.deptno,loc,DNAME,
(select count(*)
from emp where EMP.DEPTNO=DEPT.DEPTNO group by deptno
) count,
( select avg(sal)from emp where EMP.DEPTNO=DEPT.DEPTNO group by deptno
) avg
from DEPT
范例(rownum)
1.查詢工作在CHICAGO并且入職日期最早的前2,名員工姓名吕漂,入職日期亲配。
select b.hiredate,rownum from (select * from emp ORDER BY hiredate ) b
join dept on DEPT.DEPTNO=b.deptno where ROWNUM<3 and loc= 'CHICAGO'
分頁(yè)查詢
- 目標(biāo)頁(yè)數(shù)*每頁(yè)記錄數(shù)
- (目標(biāo)頁(yè)數(shù)-1)*每頁(yè)記錄數(shù)
1.每頁(yè)6條記錄,查詢第45頁(yè)高工最高的員工姓名惶凝,工資弃榨,入職日期,所在部門.
select r,ename,hiredate,sal,dname
from (select rownum r,deptno,ename,hiredate,sal from (select * from emp ORDER BY sal DESC)
where rownum<=45*6 ) s,dept where r>(45-1)*6 and DEPT.DEPTNO=s.deptno
練習(xí)
1.查詢工資高于編號(hào)7782的員工工資梨睁,并且和7396號(hào)員工工作相同的員工編號(hào)鲸睛,姓名,工資坡贺。
select sal,ename,empno
from emp
where sal>(select sal from emp where EMPNO=7782)
and job=(select job from emp where empno=7369)
2.查詢工資最高的員工姓名官辈,和工資。
select ename ,sal from emp
where sal=(select max(sal) from emp )
3.查詢部門工資高于10號(hào)部門最低工資的部門編號(hào)遍坟,部門名稱拳亿,及部門最低工資。
select emp.deptno,dname,min(sal)
from emp,DEPT
where EMP.DEPTNO=dept.DEPTNO
GROUP BY emp.deptno,dname having min(sal)<(
select min(sal) from emp where deptno=10)
4.查詢員工工資為其部門最低工資的員工編號(hào)和姓名及工資愿伴。
select ename,sal,empno from emp where sal in(
select min(sal) from emp GROUP BY deptno)
5.顯示經(jīng)理是KING的員工姓名肺魁,工資。
select ename,sal from emp
where mgr=(select empno from emp where ename='KING')
6.顯示比員工SMITH參加工作是時(shí)間晚的員工姓名隔节,工資鹅经,參加工作的時(shí)間。
select ename,sal,hiredate from emp where hiredate>
(select hiredate from emp where ename='SMITH')
7.使用子查詢方式查詢那些職員在NEW YORK工作怎诫。
select ename from emp where deptno=(select deptno from dept where loc='NEW YORK')
8.寫一個(gè)查詢顯示和員工SMITH工作在同一個(gè)部門的員工的姓名瘾晃,日期,不包括SMITH幻妓。
select ename,hiredate from emp where deptno=
(select deptno from emp where ename='SMITH')
and ename<>'SMITH'
9.寫一個(gè)查詢顯示其工資比全體職員平均工資高的員工編號(hào)蹦误,姓名。
select ename,empno from emp where sal>
(select avg(sal) from emp)
10.查詢顯示上級(jí)領(lǐng)導(dǎo)是KING的員工肉津,姓名
同第五題
11.顯示所有工作在RESEARCH部門的員工姓名强胰,職位。
select ename,job from emp join dept on dept.DEPTNO=EMP.DEPTNO
where dname='RESEARCH'
12.查詢每個(gè)部門的部門編號(hào)妹沙,平均工資偶洋,要求部門平均工資大于20號(hào)部門的平均工資。
select deptno,avg(sal) from emp GROUP BY deptno HAVING avg(sal)>(
select avg(sal) from emp where deptno=20)
13.查詢大于自己部門平均工資的員工姓名初烘,工資涡真,所在部門平均工資分俯,高于部門平均工資的額度。
select ename,sal,avg from emp,
(select deptno ,avg(sal) as avg from emp GROUP BY deptno) temp
where EMP.DEPTNO=TEMP.DEPTNO
and sal>avg
14.列出至少有一個(gè)雇員的所有部門哆料。
select deptno from emp
GROUP BY DEPTNO
having count(*)>=1
15.列出薪金比'SMITH'多的所有雇員缸剪。
select ename from emp where sal>(select sal from emp
where ename='SMITH')
16.列出入職日期早于其直接上級(jí)的所有雇員。
select EMP.ename
from emp join emp temp
on EMP.mgr=TEMP.empno
where EMP.HIREDATE<TEMP.HIREDATE
17.找員工姓名和直接上級(jí)姓名东亦。
select EMP.ename,TEMP.ename
from emp join emp temp
on EMP.MGR=TEMP.EMPNO
18.顯示每個(gè)部門的最高工資的員工杏节。
select ename from emp where sal
in(select max(sal) from emp GROUP BY DEPTNO )
19.顯示工資最高的員工參加工作時(shí)間晚的員工姓名,參加工作時(shí)間典阵。
select ename,hiredate from emp
where hiredate >(select hiredate from emp where sal=(select max(sal)from emp ))
20.顯示每位經(jīng)理管理員工的最低工資奋渔,及最低工資者的姓名。
a.SELECT ename,sal from emp
where (sal,mgr) in (SELECT min(SAL),mgr from emp GROUP BY mgr)
b.Select e.ename,e.sal From emp e,(select min(sal) mins
from emp group by mgr having mgr is not null) a where e.sal=a.mins;
21.顯示平均工資最高部門的平均部門名稱及平均工資壮啊。
a.
select DEPT.dname ,avg(sal)
from dept join emp on DEPT.DEPTNO=EMP.DEPTNO GROUP BY DEPT.dname having avg(sal)=(
select max(avg(sal)) from emp GROUP BY DEPTNO)
b.
select dept.dname,TEMPTAB2.MAXSAL
from
(select max(tempTab.avgsal) maxsal
from
(select DEPTNO,avg(sal) avgsal
from EMP
GROUP BY DEPTNO) tempTab) tempTab2
join
(select DEPTNO,avg(sal) avgsal
from EMP
GROUP BY DEPTNO) tempTab3 on tempTab2.MAXSAL = tempTab3.AVGSAL
join dept on dept.deptno = tempTab3.deptno