在select查詢語句里可以嵌入select查詢語句,稱為嵌套查詢。有些書上將內(nèi)嵌的select語句稱為子查詢,子查詢形成的結(jié)果又成為父查詢的條件。 子查詢可以嵌套多層,子查詢操作的數(shù)據(jù)表可以是父查詢不操作的數(shù)據(jù)表。子查詢中不能有order by分組語句。 4.4.1 簡單嵌套查詢 在【命令編輯區(qū)】執(zhí)行下列語句。 ――――――――――――――――――――――――――――――――――――― select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal>=(select sal from scott.emp where ename='WARD'); ――――――――――――――――――――――――――――――――――――― 單擊【執(zhí)行】按鈕,出現(xiàn)如圖4.19所示的結(jié)果。 【參見光盤文件】:/第4章/4.4/441.sql。 在這段代碼中,子查詢select sal from scott.emp where ename='WARD'的含義是從emp數(shù)據(jù)表中查詢姓名為WARD的員工的薪水,父查詢的含義是要找出emp數(shù)據(jù)表中薪水大于等于WARD的薪水的員工。上面的查詢過程等價于兩步的執(zhí)行過程。 (1)執(zhí)行“select sal from scott.emp where ename='WARD'”,得出sal=1250; (2)執(zhí)行“select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal>=1250;” 4.4.2 帶【in】的嵌套查詢 在【命令編輯區(qū)】執(zhí)行下列語句。 ――――――――――――――――――――――――――――――――――――― select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal in (select sal from scott.emp where ename='WARD'); ――――――――――――――――――――――――――――――――――――― 單擊【執(zhí)行】按鈕,出現(xiàn)如圖4.20所示的結(jié)果。 【參見光盤文件】:/第4章/4.4/442.sql。 上述語句完成的是查詢薪水和WARD相等的員工,也可以使用【not in】來進(jìn)行查詢。 4.4.3 帶【any】的嵌套查詢 在【命令編輯區(qū)】執(zhí)行下列語句。 ――――――――――――――――――――――――――――――――――――― select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal >any(select sal from scott.emp where job='MANAGER'); ――――――――――――――――――――――――――――――――――――― 單擊【執(zhí)行】按鈕,出現(xiàn)如圖4.21所示的結(jié)果。 【參見光盤文件】:/第4章/4.4/443.sql。 帶any的查詢過程等價于兩步的執(zhí)行過程。 (1)執(zhí)行“select sal from scott.emp where job='MANAGER'”,其結(jié)果如圖4.22所示。 【參見光盤文件】:/第4章/4.4/443-1.sql。 (2)查詢到3個薪水值2975、2850和2450,父查詢執(zhí)行下列語句。 【參見光盤文件】:/第4章/4.4/443-2.sql。 ―――――――――――――――――――――――――――――――――――――― select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal >2975 or sal>2850 or sal>2450; ―――――――――――――――――――――――――――――――――――――― 4.4.4 帶【some】的嵌套查詢 在【命令編輯區(qū)】執(zhí)行下列語句。
――――――――――――――――――――――――――――――――――――― select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal =some(select sal from scott.emp where job='MANAGER'); ――――――――――――――――――――――――――――――――――――― 單擊【執(zhí)行】按鈕,出現(xiàn)如圖4.23所示的結(jié)果。 【參見光盤文件】:/第4章/4.4/444.sql。 帶some的嵌套查詢與any的步驟相同。 (1)子查詢,執(zhí)行“select sal from scott.emp where job='MANAGER'”,其結(jié)果如圖4.22所示。 (2)父查詢執(zhí)行下列語句。 ――――――――――――――――――――――――――――――――――――― select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal =2975 or sal=2850 or sal=2450; ――――――――――――――――――――――――――――――――――――― 【參見光盤文件】:/第4章/4.4/444-2.sql。 帶【any】的嵌套查詢和【some】的嵌套查詢功能是一樣的。早期的SQL僅僅答應(yīng)使用【any】,后來的版本為了和英語的【any】相區(qū)分,引入了【some】,同時還保留了【any】要害詞。 4.4.5 帶【all】的嵌套查詢 在【命令編輯區(qū)】執(zhí)行下列語句。 ――――――――――――――――――――――――――――――――――――― select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal >all(select sal from scott.emp where job='MANAGER'); ――――――――――――――――――――――――――――――――――――― 單擊【執(zhí)行】按鈕,出現(xiàn)如圖4.24所示的結(jié)果。 【參見光盤文件】:/第4章/4.4/445.sql。 帶all的嵌套查詢與【some】的步驟相同。 (1)子查詢,結(jié)果如圖4.22所示。 (2)父查詢執(zhí)行下列語句。 ――――――――――――――――――――――――――――――――――――― select emp.empno,emp.ename,emp.job,emp.sal from scott.emp where sal >2975 and sal>2850 and sal>2450; ――――――――――――――――――――――――――――――――――――― 【參見光盤文件】:/第4章/4.4/445-2.sql。 4.4.6 帶【exists】的嵌套查詢 在【命令編輯區(qū)】執(zhí)行下列語句。 ――――――――――――――――――――――――――――――――――――― select emp.empno,emp.ename,emp.job,emp.sal from scott.emp,scott.dept where exists (select * from scott.emp where scott.emp.deptno=scott.dept.deptno); ――――――――――――――――――――――――――――――――――――― 單擊【執(zhí)行】按鈕,出現(xiàn)如圖4.25所示的結(jié)果。 【參見光盤文件】:/第4章/4.4/446.sql。 4.4.7 并操作的嵌套查詢 并操作就是集合中并集的概念。屬于集合A或集合B的元素總和就是并集。 在【命令編輯區(qū)】執(zhí)行下列語句。 ――――――――――――――――――――――――――――――――――――― (select deptno from scott.emp) union (select deptno from scott.dept); ――――――――――――――――――――――――――――――――――――― 單擊【執(zhí)行】按鈕,出現(xiàn)如圖4.26所示的結(jié)果。
【參見光盤文件】:/第4章/4.4/447.sql。 4.4.8 交操作的嵌套查詢 交操作就是集合中交集的概念。屬于集合A且屬于集合B的元素總和就是交集。 在【命令編輯區(qū)】執(zhí)行下列語句。 ――――――――――――――――――――――――――――――――――――― (select deptno from scott.emp) intersect (select deptno from scott.dept); ――――――――――――――――――――――――――――――――――――― 單擊【執(zhí)行】按鈕,出現(xiàn)如圖4.27所示的結(jié)果。 【參見光盤文件】:/第4章/4.4/448.sql。 4.4.9 差操作的嵌套查詢 差操作就是集合中差集的概念。屬于集合A且不屬于集合B的元素總和就是差集。 在【命令編輯區(qū)】執(zhí)行下列語句。 ――――――――――――――――――――――――――――――――――――― (select deptno from scott.dept) minus (select deptno from scott.emp); ――――――――――――――――――――――――――――――――――――― 單擊【執(zhí)行】按鈕,出現(xiàn)如圖4.28所示的結(jié)果。 【參見光盤文件】:/第4章/4.4/449.sql。 并、交和差操作的嵌套查詢要求屬性具有相同的定義,包括類型和取值范圍。