-
count(*) *代表所有的列查看全部
-
wm_concat行轉列查看全部
-
count表示求個數查看全部
-
wm_concat字符串的拼接查看全部
-
WM_CONCAT函數表示行轉列。查看全部
-
按部門統計員工的人數 函數查詢: select count(*) Total, sum(decode(to_char(hiredate,'YYYY'),'1980','1','0')) "1980", sum(decode(to_char(hiredate,'YYYY'),'1981','1','0'))"1981", sum(decode(to_char(hiredate,'YYYY'),'1982','1','0'))"1982", sum(decode(to_char(hiredate,'YYYY'),'1987','1','0')) "1987" from emp; 子查詢: select (select count(*) 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 empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) avgsal from emp e where sal>(select avg(sal) from emp where deptno=e.deptno) 多表查詢: select e.empno,e.ename,e.sal,d.avgsal from emp e,(select deptno,avg(sal) avgsal from emp group by deptno) d where e.deptno=d.deptno and e.sal>d.avgsal 相關子查詢比多表查詢占用cpu少 兩個方法的結果一樣,如何比較優劣 通過比較select語句的執行計劃: explain plan for,一般放在開頭,執行結束之后如何查看explain生成的執行計劃: select * from table(dbms_xplan.display);可以打印查看執行計劃,一般放在末尾 然后看耗費了多少CPU的執行資源,結果發現使用相關子查詢比多表查詢耗費的CPU資源要少查看全部
-
分頁查詢顯示員工信息:顯示員工號,姓名,月薪 要求:(1)每頁顯示四條記錄 (2)顯示第二頁的員工 (3)按照月薪降序排列 注意:rownum只能使用<,<=,不能使用>,>= Oracle 通過拼接子查詢方式實現分頁操作 select r,empno,ename,sal from (select rownum r,empno,ename,sal from (select rownum,empno,ename,sal from emp order by sal desc) e1 where rownum<=8) e2 where r>=5; 查看偽列的行號 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) e1 where rownum<=8) e2 where r>=5;查看全部
-
IN和not in 操作符允許我們在 WHERE 子句中規定多個值。 語句中不能有null值,否則返回空集。 所以要排除空值,判斷是否是null值,只能用is or is not而不能用= 或者!=。 Eg: a not in(10,null)相當于a!=10 and a!=null,然而a!=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 empno,ename,sal,(select avg(sal) from emp where deptno=e.deptno) avgsal from emp e where sal > (select avg(sal) from emp where deptno=e.deptno); 找出員工薪水大于本部門的平均薪水的員工查看全部
-
Top—N分析問題:按某個規律排序取出前幾項。 rownum 行號 偽列 注意:(1)行號永遠按照默認的順序生成(2)行號只能使用<,<=,不能使用>,>=. select rownum,empno,ename,sal from emp where rownum<=3 order by sal desc;(錯這個只是在emp原表中添加的行號 取出了其中的前三個) select rownum,empno,ename,sal from (select * from emp order by sal desc) where rownum<=3;查看全部
-
子查詢語法中的小括號 select * from emp where sal>(select sal from emp where ename='SCOTT');查看全部
-
子查詢注意的10個問題: (1)子查詢語法中的小括號 (2)子查詢的書寫風格(方便閱讀) (3)可以使用子查詢的位置:where,select,having,from (4)不可以使用子查詢的位置:group by (5)強調:from 后面的子查詢 (6)子查詢和主查詢可以不是同一張表 (7)一般不在子查詢中使用排序;但在Top-N分析問題中必須對子查詢排序 (8)一般先執行子查詢再執行主查詢;但是相關子查詢例外 (9)單行子查詢只能使用單行操作符;多行使用多行 (10)注意:子查詢中是null的問題查看全部
-
自連接存在的問題:不適合操作大表 解決方法:層次查詢 缺點:不存在多表查詢,查詢結果沒有自查詢直觀 select level, empno,ename,sal,mgr from emp connect by prior empno=mgr start with mgr is null;(根結點) start with mgr='7566'(任意一點) connect by 上一層的員工號=老板號查看全部
舉報
0/150
提交
取消