9、行子查詢:select empno, ename,job,deptno
from emp
where (deptno,job)=(select deptno,
job from emp where ename='smith' )
and ename<>'smith';
10、子查詢操作符:
not in() 在/不在其中
any 滿足其中任意一個
all 都滿足
11、列子查詢:select * from
emp left join saligrade
on sal between lasal and hisal
where yuangongid not in (select
lingdao id from where is not null) ;
12、any操作符(任意一個): select * from emp where sal >any(select sal from emp where deptno=30) and deptno<>30;
13、all操作符: select * from emp where sal >
all(select sal from emp where deptno=30);
14、from 子查詢:
select * from emp left join (select deptno, max(sal) mx from smp group by deptno) st on emp.deptno=st.deptno where sal=mx;
先執(zhí)行子查詢里面的聚合值產(chǎn)生列別名,在外邊where按照順序可以引用列別名;
15、select 子查詢:
select 后邊字段 是同時顯示出來的,select中不能直接引用其他字段列別名。
select * from emp;
select * from emp group by deptno;
select deptno, count(*) 各部門人數(shù),
count(*)/(select count(*) from emp) 各部門占比 from emp group by deptno;
能用單表就用單表查詢;效率高。
需要使用子查詢的情況:
①where子句中需要使用聚合函數(shù);
② 分組后的結(jié)果再分組;多次使用group by 報錯
;第一次分組后的結(jié)果為一張表;
③笛卡爾積連接的表結(jié)果再跟另一張表join連接時
④在where、group by、 having子句中使用開窗函數(shù)時;








暫無數(shù)據(jù)