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