oracle子查詢

來源:互聯網
上載者:User

標籤:


子查詢:在一個查詢的內部包含另外一個查詢。


普通子查詢


-- 查詢出比7654工資還高的所有僱員的資訊select * from emp e where e.sal > (select sal from emp where empno = 7654);-- 查詢出工資比7654高,同一時候與7788從事同樣工作的所有僱員的資訊select * from emp ewhere e.sal > (select sal from emp where empno = 7654)and e.job = (select job from emp where empno = 7788);-- 查詢出工資最低的僱員姓名、工作、工資select e.ename, e.job, e.sal from emp ewhere e.sal = (select min(sal) from emp);


in 查詢


in keyword用來匹配一個集合中的記錄

-- 查詢僱員編號為1234,2345,7369,7900的僱員資訊select * from emp where empno in(1234, 2345, 7369, 7900);

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWFnaTEyMDE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >


-- 查詢僱員編號不是 1234,2345。7369,7900的僱員資訊select * from emp where empno not in(1234, 2345, 7369, 7900);


-- 查詢每一個部門的最低工資相應的員工資訊select * from emp where sal in (select min(sal) from emp group by deptno);



anykeyword


any:表示隨意的。

< any 比子查詢返回的隨意一個結果小就可以,即小於返回結果的最大值

= any 和子查詢中隨意一個結果相等就可以。相當於in

> any 比子查詢返回的隨意一個結果大就可以,即大於返回結果的最小值


-- 查詢每一個部門的最低工資select min(sal) min_sal from emp group by deptno;


sal 大於 any (每一個部門最低工資),即大於返回結果的最小值

select * from emp where sal > any (select min(sal) from emp group by deptno);

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWFnaTEyMDE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >


sal = any (每一個部門最低工資),即 和子查詢中每一個結果相等,同in

select * from emp where sal = any (select min(sal) from emp group by deptno);

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWFnaTEyMDE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >


sal < any (每一個部門最低工資)。即大於返回結果的最大值

select * from emp where sal < any (select min(sal) from emp group by deptno);



allkeyword


all:表示全部的。

< all 比子查詢返回的全部的結果都小,即小於返回結果的最小值

> all 比子查詢返回的全部的結果都大,即大於返回結果的最大值

= all 無意義。邏輯上也不成立


查詢工資在2000 到 3500的工資段的工資集合

select distinct sal from emp where sal between 2000 and 3500;


> all (工資在2000 到 3500的工資段的工資集合) ,即大於最大值

select * from emp where sal > all(select distinct sal from emp where sal between 2000 and 3500);

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWFnaTEyMDE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" >


< all (工資在2000 到 3500的工資段的工資集合),即小於最小值

select * from emp where sal < all(select distinct sal from emp where sal between 2000 and 3500);




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.