標籤:
子查詢:在一個查詢的內部包含另外一個查詢。
普通子查詢
-- 查詢出比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子查詢