oracle進階查詢(執行個體基於scott使用者四張表)

來源:互聯網
上載者:User

標籤:連結   報表   order   star   分頁   to_char   tar   名稱   ora   

oracle進階查詢(執行個體基於scott使用者四張表)

分組查詢

多表查詢

子查詢

綜合執行個體

=======================================================================

scott使用者的四張表(emp,dept,bonus,salgrade)

沒有這四張表的可參考http://blog.csdn.net/love_legain/article/details/54311040進行建立

-------------------------------------------

desc emp

名稱 空值 類型

-------- -------- ------------

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;








oracle進階查詢(執行個體基於scott使用者四張表)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.