Oracle-子查詢

來源:互聯網
上載者:User

標籤:where   from   子查詢   

一、WHERE條件中的子查詢

1. 比black工資高的僱員有哪些?

select ename 

from emp

where sal>(select sal from emp where ename=‘BLAKE‘);


2. 高於30部門最高工資的僱員有哪些?

select ename,sal 

from emp

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


select ename,sal 

from emp

where sal > all (select sal from emp where deptno=10);   --任何


3. 當all後面接子查詢的時候

"x = ALL (...)": The value must match all the values in the list to evaluate to TRUE.所有值都要匹配

"x != ALL (...)": The value must not match any values in the list to evaluate to TRUE.至少有一個值不匹配

"x > ALL (...)": The value must be greater than the biggest value in the list to evaluate to TRUE.大於最大的值

"x < ALL (...)": The value must be smaller than the smallest value in the list to evaluate to TRUE.小於最小的值

"x >= ALL (...)": The value must be greater than or equal to the biggest value in the list to evaluate to TRUE.大於等於最大的值

"x <= ALL (...)": The value must be smaller than or equal to the smallest value in the list to evaluate to TRUE.小於等於最小的值


4. 大於10部門最小工資的僱員有哪些?

select ename,sal 

from emp

where sal> (select min(sal) from emp where deptno=10);


select ename,sal 

from emp

where sal > any (select sal from emp where deptno=10);   --any 大於任何一個,那不就是最小的嗎??,任意一個


5. 當any後面接子查詢的時候

"x = ANY (...)": The value must match one or more values in the list to evaluate to TRUE.至少匹配一個值

"x != ANY (...)": The value must not match one or more values in the list to evaluate to TRUE.一個值都不匹配

"x > ANY (...)": The value must be greater than the smallest value in the list to evaluate to TRUE.大於最小值

"x < ANY (...)": The value must be smaller than the biggest value in the list to evaluate to TRUE.小於最大值

"x >= ANY (...)": The value must be greater than or equal to the smallest value in the list to evaluate to TRUE.大於等於最小值

"x <= ANY (...)": The value must be smaller than or equal to the biggest value in the list to evaluate to TRUE.小於等於最大值


6. 工資最高的人是誰?

select ename from emp

where sal=(select max(sal) from emp);


7. 和ALLEN同部門,工資高於MARTIN的僱員有哪些?

select ename from emp

where deptno=(select deptno from emp where ename=‘ALLEN‘)

and sal>(select sal from emp where ename=‘MARTIN‘);


8. 工作和部門與SMITH相同,工資高於JAMES的僱員有哪些?

select ename from emp

where (job,deptno)=(select job,deptno from emp where ename=‘SMITH‘)

and sal>(select sal from emp where ename=‘JAMES‘);



二、FROM子句中的子查詢

1. 工資高於本部門平均工資的人(拿上遊工資的人)有哪些?

    ①求出每個部門的平均工資,把這個作為一張表

    ②使用emp表和平均工資表進行關聯,

select ename,sal,avgsal,e.deptno

from emp e,

(select avg(sal) avgsal,deptno 

from emp

group by deptno) b

where e.deptno=b.deptno

and e.sal>b.avgsal;


三、偽列:rownum 

特點:先有結果集在有rownum,是對結果集的一個編號


1. 工資前五名的人?(TOP-N 分析)

    ①先把工資排序

    ②在使用rownum限結果集(為什麼不在第一步就使用rownum限定結果集?執行順序的問題,where要比order by先執行,擷取rownum<6的時候還沒來得及排序在從emp裡面拿出來

select ename,sal

from emp

where sal in

(select sal 

from (select distinct sal from emp order by sal desc)

where rownum<6)

order by sal desc;


3. 工資6~10的人?

    ①先把工資排序

    ②把工資排名在6~10的拿出來,由於不能使用rownum>6 and xxx<10這樣,所以要加一步,把rownum變成id列,這樣就又構造成一個結果集

    ③把上一個結果集中id為6~10的條目拿出來

    ④和emp關聯

select ename,sal from emp 

where sal in 

(select sal from 

(select rownum rn,sal 

from (select distinct sal 

      from emp order by sal desc))

where rn between 6 and 10)

order by sal desc;

本文出自 “菜鳥達人” 部落格,請務必保留此出處http://omphy.blog.51cto.com/7921842/1895372

Oracle-子查詢

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.