oracle高級查詢(實例基於scott用戶四張表)sql
分組查詢oracle
多表查詢less
子查詢函數
綜合實例spa
=======================================================================.net
scott用戶的四張表(emp,dept,bonus,salgrade)code
沒有這四張表的可參考http://blog.csdn.net/love_legain/article/details/54311040進行建立blog
-------------------------------------------排序
desc empit
名稱 空值 類型
-------- -------- ------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
---------------------------------------------
desc dept
名稱 空值 類型
------ -------- ------------
DEPTNO NOT NULL NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
------------------------------------------------
desc salgrade
名稱 空值 類型
----- -- ------
GRADE NUMBER
LOSAL NUMBER
HISAL NUMBER
------------------------------------------------
desc bonus
名稱 空值 類型
----- -- ------------
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
SAL NUMBER
COMM NUMBER
=============================分組查詢==================================
①分組函數的概念
分組函數做用於一組數據,並對一組數據返回一個值
②分組函數的使用
--select AVG(sal),sum(sal) from emp;
-- select max(sal),min(sal) from emp;
--select count(*) from emp;
--select count (distinct DEPTNO) from emp;
wm_concat:行轉列
select deptno,wm_concat(ename) from emp group by deptno--11gr2和12C上已經摒棄了wm_concat函數
在分組函數中使用nvl函數:nvl函數使分組函數沒法忽略空值
select count(*),count(NVL(comm,0)) from emp;
③使用group by子句數據分組
select deptno,avg(sal) from emp group by deptno;
注意:在select列表中全部未包含在組函數中的列都應該包含在group by子句中
包含在group by子句中的列沒必要包含在select列表中
④使用having子句過濾分組結果集
不能再where子句中使用分組函數
能夠在having子句中使用分組函數
select deptno,avg(sal) from emp group by deptno having deptno=10;
select deptno,avg(sal) from emp where deptno=10 group by deptno;
⑤在分組查詢中使用order by子句
⑥group by語句得加強
select deptno,job,sum(sal) from emp group by deptno,job;
+
select deptno,sum(sal) from emp group by deptno;
+
select sum(sal) from emp;
=
select deptno,job,sum(sal) from emp group by rollup(deptno,job);
sql*plus的報表功能
================================多表查詢================================
①什麼是多表查詢
從多個表中獲取數據
②笛卡爾積
③等值鏈接
select e.empno,e.ename,e.sal,d.dname from emp e,dept d where e.deptno=d.deptno;
④不等值鏈接
select e.empno,e.ename,e.sal,s.grade from emp e,salgrade s where e.sal between s.losal and s.hisal;
⑤外鏈接
核心:經過外連接,把連接不成立的記錄,任然包含在最後的結果中
左外鏈接:當鏈接條件不成立的時候,等號左邊的表依然被包含
右外鏈接:當鏈接條件不成立的時候,等號右邊的表依然被包含
select d.deptno 部門號,d.dname 部門名稱,count(e.empno) 人數 from emp e,dept d where e.deptno(+)=d.deptno group by d.deptno,d.dname;--右外鏈接
⑥自鏈接
核心:經過別名,將同一張表視爲多張表
select e.ename 員工姓名,b.ename 老闆姓名 from emp e,emp b where e.mgr=b.empno;
自鏈接存在的問題:不適合操做大表
⑦解決方法:層次查詢
select level,empno,ename,sal,mgr from emp connect by prior empno=mgr start with mgr is null order by 1;
==============================子查詢===================================
①子查詢概述
--查詢比scott工資高的員工信息
select * from emp where sal>(select sal from emp where ename='scott');
②子查詢的使用
能夠使用子查詢的位置:where,select,having,from
主查詢和子查詢能夠不是同一張表
select * from emp where deptno=(select deptno from dept where dname='SALES');
select e.* from emp e,dept d where e.deptno=d.deptno and d.dname='SALES';
通常不在子查詢中,使用排序,但在top-n分析問題中,必須對子查詢排序
--rownum 行號 僞列
select rownum,empno,ename,sal from (select * from emp order by sal desc) where rownum <= 3;
行號永遠按照默認的順序生成
行號只能使用<,<=;不能使用>,>=
通常先執行子查詢,再執行主查詢,但相關子查詢除外
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);
單行子查詢和多行子查詢
操做符(多行)
in 等於列表中的任何一個
any 和子查詢返回的任意一個值比較
all 和子查詢返回的全部值比較
操做符(單行)
= equal to
> greater than
>=greater than or equal to
<less than
<= less than or equal to
<>not equal to
select * from emp where job=(select job from emp where empno=7566) and sal >(select sal from emp where empno=7782);
select * from emp where sal=(select min(sal) from emp);
--查詢最低工資大於20號部門最低工資的部門號和部門的最低工資
select deptno,min(sal) from emp group by deptno having min(sal) > (select min(sal) from emp where deptno=20);
--查詢部門名稱是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');
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);
子查詢對控制問題
--查詢不是老闆的員工
select * from emp where empno not in (select mgr from emp where mgr is not null);
===================================綜合實例=============================
實例一
分頁查詢顯示員工信息:顯示員工號,姓名,月薪
-每頁顯示四條記錄
-顯示第二頁的員工
-按照月薪降序排列
select r,emp,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;
--oracle分頁經過子查詢實現
實例二
找到員工表中薪水大於本部門平均薪水的員工
select e.empno,e.name,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;
實例三
按部門統計員工人數,按照以下格式輸出(員工的入職年份已知)
total 1980 1981 1982 1987
14 1 10 1 2
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"
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"
from dual;