1. 程式人生 > >oracle入門之對表數據查詢(三)

oracle入門之對表數據查詢(三)

取值 無法 排除 avg 臨時表 多表 單行 pan 相同

oracle表復雜查詢--子查詢

什麽是子查詢?

子查詢是指嵌入在其它sql語句中的select語句,也叫嵌套查詢。

單行子查詢

單行子查詢是指只返回一行數據的子查詢語句。

請思考:如果顯示與smith同一部門的所有員工?

SQL>select * from emp where deptno=(select deptno from emp where ename=‘SMITH‘);

SMITH排除在外不顯示

SQL>select * from emp where deptno=(select deptno from emp where ename=‘SMITH‘) and ename<>‘SMITH‘;

SQL>select * from emp where deptno=(select deptno from emp where ename=‘SMITH‘) and ename!=‘SMITH‘;

多行子查詢?

多行子查詢指返回多行數據的子查詢。

請思考:如何查詢和部門10的工作相同的雇員的名字、崗位、工資、部門號

SQL>select ename,job,sal,deptno from emp where job in (select distinct job from emp where deptno=10);

特別註意:多行子查詢是不能使用=號的,=號是單行子查詢(由於只返回一個結果所以使用=號),多行子查詢返回的不是一個結果所以要使用in。

oracle表復雜查詢--子查詢

在多行子查詢中使用all操作符

請思考:如何顯示工資比部門30的所有員工的工資高的員工的姓名、工資和部門號

SQL>select ename,sal,deptno from emp where sal>all(select sal from emp where deptno=30);

擴展要求:大家想想還有沒有別的查詢方法?

SQL>select ename,sal,deptno from emp where sal>(select max(sal) from emp where deptno=30);

在多行子查詢中使用any操作符

請思考:如何顯示工資比部門30的任意一個員工的工資高的員工的姓名、工資和部門號

SQL>select ename,sal,deptno from emp where sal>any(select sal from emp where deptno=30);

擴展要求:大家想想還有沒有別的查詢方法?

SQL>select ename,sal,deptno from emp where sal>(select min(sal) from emp where deptno=30);

多列子查詢

單行子查詢是指子查詢只返回單列、單行數據,多行子查詢是指返回單列多行數據,都是針對單列而言的,而多列子查詢則是指查詢返回多個列數據的子查詢語句。

請思考如何查詢與smith的部門和崗位完全相同的所有雇員?

原始查詢方法(不推薦)

多表多列查詢:

SQL>select * from emp e,dept d where e.deptno=d.deptno and d.dname=(select d.dname from emp e,dept d where e.deptno=d.deptno and e.ename=‘SMITH‘) and e.job=(select job from emp where ename=‘SMITH‘);

單表多列查詢:

SQL>select * from emp where deptno=(select deptno from emp where ename=‘SMITH‘) and job=(select job from emp where ename=‘SMITH‘);

多列查詢(優化方法,在oracle下推薦使用)

SQL>select * from emp where (deptno,job)=(select deptno,job from emp where ename=‘SMITH‘);

特別註意:查詢的列要與返回的列名相對應,順序不能出錯。否報錯。

oracle表復雜查詢--子查詢

from子句中使用子查詢**此知識點必需掌握**

請思考:如何顯示高於自己部門平均工資的員工的信息

這裏要用到數據查詢的小技巧,把一個子查詢當作一個臨時表使用。

SQL>select e.ename,e.sal,t1.myavg,e.deptno from emp e,(select avg(sal) myavg,deptno from emp group by deptno) t1 where e.deptno=t1.deptno and e.sal>t1.myavg order by e.deptno;

下面方法不推薦使用(無法取出子查詢(臨時表)的值)

SQL>select e.* from emp e where e.sal>(select avg(sal) from emp where deptno=e.deptno);

from子句中使用子查詢

請思考:查找每個部門工資最高的人的詳細資料

思路:得到所有的員工,進行篩選,每拿到一個員工,判斷該員工的工資是否是他們部門的最高工資。

SQL>select e.*,t1.mysal from emp e,(select max(sal) mysal,deptno from emp group by deptno) t1 where e.deptno=t1.deptno and e.sal>=t1.mysal order by e.deptno;

SQL>select * from emp where sal in (select max(sal) from emp group by deptno) order by deptno;

SQL>select * from emp e where sal=(select max(sal) from emp where deptno=e.deptno) order by deptno;

from子句中使用子查詢

請思考:顯示每個部門的信息(編號、名稱)和人員數量,我們一起完成。

SQL>select distinct d.deptno,d.dname,t1.myuser from dept d,(select deptno,count(*) myuser from emp group by deptno) t1 where d.deptno=t1.deptno(+) order by d.deptno;

SQL>select distinct d.deptno,d.dname,t1.myuser from dept d left join (select deptno,count(*) myuser from emp group by deptno) t1 on d.deptno=t1.deptno order by d.deptno;

from子句中使用子查詢

這裏需要說明的當在from子句中使用子查詢時,該子查詢會被作為一個臨時表來對待,當在from子句中使用子查詢時,必需給予查詢指定別名。

oracle表復雜查詢--子查詢

分頁查詢是我們學習任何一種數據庫,必需掌握的一個要點。

oracle數據庫的分頁查詢說明:

特別註意:oracle每張表都有rownum默認字段,默認情況下是不顯示的。但是是一直存在的。

SQL>select t2.* from (select t1.*,rownum rn from (select * from emp) t1 where rownum<=6) t2 where rn>=4;

解釋:

select * from emp //為第一張表,查詢表中的所有數據

select t1.*,rownum rn from (select * from emp) t1 where rownum<=6 //為第二張表,把表一查到的數據看成一張臨時表從中取出前6條數據,並形成第二張臨時表。

select t2.* from (select t1.*,rownum rn from (select * from emp) t1 where rownum<=6) t2 where rn>=4; //為第三張表,把表二中取到的6條數據看成臨時表,並從這6條數據中從第4條取到第6條數據,再形成第三張臨時表,也就是我們所要查詢的最終結果。

分頁查詢語法模版

select t2.* from (select t1.*,rownum rn from (select * from 表名) t1 where rownum<=大範圍(取到多少條數據)) t2 where rn>=小範圍(從第幾條數據開始取);

特別說明:oracle分頁查詢是通過三層篩選法進行查詢的。每一次都可以帶where條件來對要查詢的信息進行篩選。

建議:針對不同情況分頁查詢,盡可能的在最內層(第一層)設置條件,包括多表分頁;第二層設置最大取值範圍;第三層從第幾條數據開始取值。

上面的這個sql分頁查詢模版是oracle數據庫效率比較高的查詢方法,在百萬數據級別對數據進行查詢都可以及時響應。

oracle入門之對表數據查詢(三)