-
相關子查詢:查詢員工表中薪水大于本部門平均薪水的員工
select empno ,ename,sal,(select avg(sal) from emp where deptno=e.deptno)avg
from emp e
where sal>(select avg(sal) from emp where deptno=e.deptno);
查看全部 -
行號永遠按照默認順序生成
行號只能使用<,<=;不能使用>.>=;
實例:查詢emp表中薪水排行前三的員工信息
select rownum,empno,ename,sal
from (select * from emp order by sal desc) where ROWNUM<=3;
查看全部 -
多表查詢和子查詢,最好使用多表查詢,因為子查詢需要訪問兩次數據庫,而多表查詢只需訪問一次
查看全部 -
group by 后面不能使用子查詢
查看全部 -
查詢工資比scott高的員工的工資;
select *?
from emp
where sal>(select sal from emp where ename='SCOTT');
查看全部 -
層次查詢:“connect by prior...”語句
select level, empno,ENAME,sal,MGR
from emp
connect by prior empno=mgr
start with mgr is null
order by 1;
查看全部 -
外鏈接:
(1)左外連接:“(+)”放在等號的右邊;
(2)右外連接:“(+)”放在等號的左邊;
查看全部 -
rollup(a,b)函數
查看全部 -
having語句用于過濾group by語句后的結果 放在group bu語句的后面查看全部
-
group by 語句的增強 rollup查看全部
-
使用count函數計算某列的數量時不會算上為空的字段查看全部
-
8.一般先執行子查詢,再執行主查詢;但相關子查詢例外
(1)**相關子查詢 (子查詢使用了主查詢字段)
select 員工號,姓名,工資,
(select avg(工資) from 員工表 where 部門號 = t1.部門號) 部門平均工資
from 員工表 t1
where 工資 > (select avg(工資)
? from 員工表?
? where 部門號 = t1.部門號)
查看全部 -
>>子查詢注意的10個問題
1.子查詢語法中的小括號
2.子查詢的書寫風格
3.可以使用子查詢的位置:where,select,having,from
4.不可以使用子查詢的位置:group by
5.強調:from后面的子查詢
6.主查詢和子查詢可以不是同一張表
7.一般不在子查詢中,使用排序;但在Top-N分析問題中,必須對子查詢排序
8.一般先執行子查詢,再執行主查詢;但相關子查詢例外
9.單行子查詢只能使用單行操作符;多行子查詢只能使用多行操作符
10.注意:子查詢中是null值得問題
查看全部 -
>>子查詢注意的10個問題
1.子查詢語法中的小括號
2.子查詢的書寫風格
3.可以使用子查詢的位置:where,select,having,from
(1) select 必須是單行子查詢
select 工號,姓名,工資,(select 職位 from 員工表 where 工號='100')?
from 員工表
(2) having
select 部門號,ags(工資)
from 員工表
group by 部門號
having ags(工資) > (select max(工資)
from 員工表
where 部門號 = 30)
(3)from
select *
from(select 員工號,姓名,工資?
from 員工表)
4.不可以使用子查詢的位置:group by
5.強調:from后面的子查詢
select *
from (select 員工號,姓名,工資 月薪,工資*12 年薪 from 員工表)
6.主查詢和子查詢可以不是同一張表
--子查詢
select *
from 員工表
where 部門號 = (select 部門號?
from 部門表?
where 部門名稱 = '銷售部')
--多表查詢
select t1.*?
from 員工表 t1,部門表 t2
where t1.部門號 = t2.部門號?
? and t2.部門名稱 = '銷售部'
7.一般不在子查詢中,使用排序;但在Top-N分析問題中,必須對子查詢排序
**rownum 行號,偽列
*行號永遠按照默認的順序生成
*行號只能使用 <,<=;不能使用 >,>=
select rownum,員工號,姓名,工資?
from 員工表
(1)top-n 前三名
select rownum,員工號,姓名,工資
from(select *
from 員工表
order by 工資 desc)
where rownum <= 3
8.一般先執行子查詢,再執行主查詢;但相關子查詢例外
(1)**相關子查詢 (子查詢使用了主查詢字段)
select 員工號,姓名,工資,
(select avg(工資) from 員工表 where 部門號 = t1.部門號) 部門平均工資
from 員工表 t1
where 工資 > (select avg(工資)
? from 員工表?
? where 部門號 = t1.部門號)
? ? --
select 員工號,姓名,工資
from 員工表 t1
where 工資 > (select 部門號,avg(工資)
? from 員工表?
? group by 部門號
? having 部門號 = t1.部門號)
--
select t1.員工號,t1.姓名,t1.工資,t2.部門平均工資
from 員工表 t1,
(select 部門號,avg(工資) 部門平均工資
from 員工表
group by 部門號) t2
where t1.部門號 = t2.部門號
? and t1.工資 > t2.部門平均工資
9.單行子查詢只能使用單行操作符;多行子查詢只能使用多行操作符
多行操作符 in/any/all
--in
--any
select *
from 員工表
where 工資 > any(select 工資
? ? ?from 員工表
? ? ?where 部門號 = 30)
--等價
select *
from 員工表
where 工資 > (select min(工資)
? from 員工表
? where 部門號 = 30) ??
--all
select *
from 員工表
where 工資 > all(select 工資
? ? ?from 員工表
? ? ?where 部門號 = 30)
--等價
select *
from 員工表
where 工資 > (select max(工資)
? from 員工表
? where 部門號 = 30) ??
10.注意:子查詢中是null值得問題
單行子查詢null值問題
多行子查詢null值問題
**not in
a not in (10,20,null)
a != 10 and a != 20 and a != null
all
select *
from 員工表
where 員工號 not in (select 老板號
from 員工表
where 老板號 not null)
查看全部 -
select *
from emp
where sal>(select sal
? ? ? ? ? ? ? ? ? ? from emp
? ? ? ? ? ? ? ? ? ? ?where ename='SCOTT');
查看全部
舉報