-
子查詢實例
查看全部 -
select
(select? count(*) total from emp) total,
(select count(*) from emp where to_char(hiredate,'YYYY')='1980') "1980",
(select count(*) from emp where to_char(hiredate,'YYYY')='1981') "1981",
(select count(*) from emp where to_char(hiredate,'YYYY')='1982') "1982",
(select count(*) from emp where to_char(hiredate,'YYYY')='1987') "1987"
from dual
查看全部 -
select? count(e.empno) total,
sum(decode(to_char(e.hiredate,'YYYY'),'1980',1,0)) "1980",
sum(decode(to_char(e.hiredate,'YYYY'),'1981',1,0)) "1981",
sum(decode(to_char(e.hiredate,'YYYY'),'1982',1,0)) "1982",
sum(decode(to_char(e.hiredate,'YYYY'),'1987',1,0)) "1987"
from emp? e
查看全部 -
1oracle分頁是通過嵌套子查詢完成的
select rownum,r,empno,ENAME,sal from?
(select rownum r , empno,ENAME,sal
from (select rownum,empno,ENAME,sal from emp? ?order by sal desc ) a1? where? rownum<=8) a2
where r>=5
查看全部 -
--執行計劃:explain plan for select .......
--查看執行計劃:select * from table(dbms_xplan.display);
explain plan for
(
select *?
from emp e,(select e.deptno,avg(e.sal) avgsal from emp e group by e.deptno) d
where e.deptno=d.deptno and e.sal>d.avgsal
)
select * from table(dbms_xplan.display);
查看全部 -
select *?
from emp e
where e.empno not in
(select mgr from emp where? mgr is not null)?
查看全部 -
子查詢的類型 (1)單行子查詢:只返回一條記錄 ? ?操作符:=,>,>=,<,<=,<>不等于 例子:查詢員工信息,要求:職位與7566員工一樣,薪水大于7782的 ?select * from emp where job= (select job from emp where empno=7566) and ?sal>(select sal from emp where empno=7782); ? ? (2)多行子查詢:返回多條記錄 ? ? 操作符:in 等于列表中的任何一個 ? ? ? ? ? ?any 和子查詢返回的任意一個值作比較 ? ? ? ? ? ?all 和子查詢返回的所有值比較 例子:(1)in ?查詢部門名稱是sales 和accounting 的員工信息 ? select *from emp where deptno in (select deptno from dept where dname='SALES' or dname='ACCOUNTING'); ? select e.* from emp e, dept d where e.deptno=d.deptno and (d.dname='SALES' or d.dname='ACCOUNTING'); ? ?(2)any ?查詢工資比30號部門任意一個員工高的員工信息 ? ? ?select * from emp where sal> any ( select sal from emp where deptno=30); ? ? ?select * from emp where sal>(select min(sal) from emp where deptno=30); ? ?(3)all ?查詢工資比30號部門所有一個員工高的員工信息 ? ? ?select * from emp where sal> all( select sal from emp where deptno=30); ? ? ?select * from emp where sal>(select max(sal) from emp where deptno=30);
查看全部 -
--趙春各部門薪資大于平均薪資的員工? (相關子查詢)
select e.*, (select avg(sal) from emp where deptno=e.deptno) avgSal
from emp e
where e.sal>(select avg(sal) from emp where deptno=e.deptno)
查看全部 -
--TOP-N問題分析
--rownum是oracle偽列? 是在生成的時候就默認存在的行號? 不會因為我們的排序發生改變??
select rownum,e.sal?
from emp e
where rownum <=3
order by? e.sal desc
--利用子查詢的時候 新生成的行號rownum找到薪資前三名的員工信息
select rownum, info.*
from (
select * from emp e
order by e.sal desc
?) info
?where rownum<=3
查看全部 -
--查詢銷售部的員工信息
--where 子查詢?
select *?
from emp?
where deptno=(select deptno from dept t where t.dname='SALES')
--多表查詢(理論上更優? 只有一個from 一次操作數據庫)
select * from emp e,dept d
where e.deptno=d.deptno and d.dname='SALES'
查看全部 -
from 后面可以是表,也可以是查詢語句
查看全部 -
--where 子查詢
select *
from emp?
where sal > (select sal
? ? ? ? ? ? ?from emp
? ? ? ? ? ? ?where ename = 'SCOTT' );
??
--select 子查詢? ? ? ? ? ?
?select ename,(select job from emp where empno=7839)?
?from emp
?
?--from 子查詢
?select ename?
?from (select * from emp)
?
?
?--having 子查詢
?select deptno,avg(sal)
?from emp?
?group by deptno
?having avg(sal)>(select avg(sal) from emp where deptno=20)
?
查看全部 -
1可以使用子查詢的位置:where,select,having,from
2不可以使用子查詢的位置:group by
3主查詢和子查詢可以不是同一張表?
4一般不在子查詢中,使用排序;但在Top-N分析問題中,必須對子查詢排序?
5一般先執行子查詢,再執行主查詢;但相關子查詢例外?
6單行子查詢只能使用單行操作符;多行子查詢只能使用多行操作符?
7注意:子查詢中是null值問題
查看全部 -
例:查詢工資比Scott高的員工?
select * from emp where sal > (select sal ? ? ? ? ? ? from emp ? ? ? ? ? ? where ename = 'SCOTT' );
查看全部 -
自連接缺陷:多表查詢》》笛卡爾集》》》會帶來至少平方級別的數據累計(不適合大表)
引出層次查詢(實際是是單表查詢? 沒有笛卡爾集)
需要遍歷樹結構?
節點連接條件(上一節點的員工號是下一節點的老板)connect by prior?empno=mgr
需要設置開始節點 start with
各有優缺點:自連接直觀(但不適合大表) 層次查詢性能更好(但是不直觀)
查看全部
舉報