oracle子查詢,oracle子

來源:互聯網
上載者:User

oracle子查詢,oracle子


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


普通子查詢


-- 查詢出比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 關鍵字用來匹配一個集合中的記錄

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



-- 查詢僱員編號不是 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);



any關鍵字


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);



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

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


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

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



all關鍵字


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);



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

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




著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

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.