子查詢
SQL> */
子查詢 (內查詢) 在主查詢之前一次執行完成。 子查詢的結果被主查詢使用 (外查詢)。
SELECTlast_name,job_id<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPjxzdHJvbmc+LHNhbGFyeTwvc3Ryb25nPjwvcD4KPHA+CjxzdHJvbmc+RlJPTSAgIGVtcGxveWVlczwvc3Ryb25nPjwvcD4KPHA+CjxzdHJvbmc+V0hFUkUgCjwvc3Ryb25nPjxzdHJvbmc+am9iX2lkPC9zdHJvbmc+PHN0cm9uZz49IAo8L3N0cm9uZz48L3A+CjxwPgo8c3Ryb25nPiAgICAgICAgICAgICAgICAoU0VMRUNUCjwvc3Ryb25nPjxzdHJvbmc+am9iX2lkPC9zdHJvbmc+PC9wPgo8cD4KPHN0cm9uZz4gICAgICAgICAgICAgICAgIEZST00gICBlbXBsb3llZXM8L3N0cm9uZz48L3A+CjxwPgo8c3Ryb25nPiAgICAgICAgICAgICAgICAgV0hFUkUgCjwvc3Ryb25nPjxzdHJvbmc+ZW1wbG95ZWVfaWQ8L3N0cm9uZz48c3Ryb25nPj0gMTQxKTwvc3Ryb25nPjwvcD4KPHA+CjxzdHJvbmc+QU5EICAgIHNhbGFyeSAmZ3Q7PC9zdHJvbmc+PC9wPgo8cD4KPHN0cm9uZz4gICAgICAgICAgICAgICAgKFNFTEVDVCBzYWxhcnk8L3N0cm9uZz48L3A+CjxwPgo8c3Ryb25nPiAgICAgICAgICAgICAgICAgRlJPTSAgIGVtcGxveWVlczwvc3Ryb25nPjwvcD4KPHA+CjxzdHJvbmc+ICAgICAgICAgICAgICAgICBXSEVSRSAKPC9zdHJvbmc+PHN0cm9uZz5lbXBsb3llZV9pZDwvc3Ryb25nPjxzdHJvbmc+PSAxNDMpOzwvc3Ryb25nPjwvcD4KPGJyPgoKPGgyPrbg0NDX07Lp0a88L2gyPgo8YnI+Cgo8aDM+aW7T625vdCBpbjwvaDM+ClNRTCZndDsgLS224NDQ19Oy6dGvPGJyPgpTUUwmZ3Q7IC0taW46ICDU2ryvus/W0Dxicj4KU1FMJmd0OyAtLbLp0a+yv8PFw/uzxs6qU0FMRVO6zUFDQ09VTlRJTke1xNSxuaTQxc+iPGJyPgpTUUwmZ3Q7IC0twbfPsKO6tuCx7bLp0a88YnI+ClNRTCZndDsgc2VsZWN0ICo8YnI+CiAgMiAgZnJvbSBlbXA8YnI+CiAgMyAgd2hlcmUgZGVwdG5vIGluICAgKHNlbGVjdCBkZXB0bm8gZnJvbSBkZXB0IHdoZXJlIGRuYW1lPQ=="SALES' or dname='ACCOUNTING'); not in相當於all的范圍,即非集合中的所有,所以集合中出現null時,返回空,即結果集為空集! in相當於any的范圍,即在集合中的任意一個元素符合即可,所以即使集合中出現null,也不妨礙結果集!
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7839 KING PRESIDENT 17-11月-81 5000 10
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7900 JAMES CLERK 7698 03-12月-81 950 30
7934 MILLER CLERK 7782 23-1月 -82 1300 10
已選擇9行。
SQL> ed
已寫入 file afiedt.buf
1 select *
2 from emp
3* where deptno not in (select deptno from dept where dname='SALES' or dname='ACCOUNTING')
SQL> /
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 800 20
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7788 SCOTT ANALYST 7566 13-7月 -87 3000 20
7876 ADAMS CLERK 7788 13-7月 -87 1100 20
7902 FORD ANALYST 7566 03-12月-81 3000 20
SQL> host cls
any與all查詢
any
SQL> --any: 集合中的任意一個
SQL> --查詢工資比20號部門任意員工工資高的員工信息
SQL> select *
2 from emp
3 where sal > any (select sal from emp where deptno=20);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7839 KING PRESIDENT 17-11月-81 5000 10
7902 FORD ANALYST 7566 03-12月-81 3000 20
7788 SCOTT ANALYST 7566 13-7月 -87 3000 20
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7934 MILLER CLERK 7782 23-1月 -82 1300 10
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7876 ADAMS CLERK 7788 13-7月 -87 1100 20
7900 JAMES CLERK 7698 03-12月-81 950 30
已選擇13行。
SQL> ed
已寫入 file afiedt.buf
1 select *
2 from emp
3* where sal > any (select sal from emp where deptno=10)
SQL> /
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7839 KING PRESIDENT 17-11月-81 5000 10
7902 FORD ANALYST 7566 03-12月-81 3000 20
7788 SCOTT ANALYST 7566 13-7月 -87 3000 20
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
已選擇8行。
all
SQL> --all:和集合的所有值比
SQL> --查詢工資比20號部門所有員工工資高的員工信息
SQL> select *
2 from emp\
3 ;
from emp\
*
第 2 行出現錯誤:
ORA-00911: 無效字符
SQL> select *
2 from emp
3 where sal > all (select sal from emp where deptno=20);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7839 KING PRESIDENT 17-11月-81 5000 10
SQL> select *
2 from emp
3 where sal > (select min(sal) from emp where deptno=10);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7788 SCOTT ANALYST 7566 13-7月 -87 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7902 FORD ANALYST 7566 03-12月-81 3000 20
已選擇8行。
SQL> ed
已寫入 file afiedt.buf
1 select *
2 from emp
3* where sal > (select max(sal) from emp where deptno=20)
SQL> /
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7839 KING PRESIDENT 17-11月-81 5000 10
SQL> host cls
SQL> --查詢不是經理的員工
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7369 SMITH CLERK 7902 17-12月-80 800 20
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
7788 SCOTT ANALYST 7566 13-7月 -87 3000 20
7839 KING PRESIDENT 17-11月-81 5000 10
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7876 ADAMS CLERK 7788 13-7月 -87 1100 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7900 JAMES CLERK 7698 03-12月-81 950 30
7902 FORD ANALYST 7566 03-12月-81 3000 20
7934 MILLER CLERK 7782 23-1月 -82 1300 10
已選擇14行。
關於not in與in後面的空值問題
2 from emp
3 where empno not in (select mgr from emp);
未選定行
SQL> --查詢是經理的員工
SQL> ed
已寫入 file afiedt.buf
1 select *
2 from emp
3* where empno in (select mgr from emp)
SQL> /
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7902 FORD ANALYST 7566 03-12月-81 3000 20
7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
7839 KING PRESIDENT 17-11月-81 5000 10
7566 JONES MANAGER 7839 02-4月 -81 2975 20
7788 SCOTT ANALYST 7566 13-7月 -87 3000 20
7782 CLARK MANAGER 7839 09-6月 -81 2450 10
已選擇6行。
SQL> select *
2 from emp
3 where empno not in (select mgr from emp where mgr is not null);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------------- ----- ---------- ----------
7844 TURNER SALESMAN 7698 08-9月 -81 1500 0 30
7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
7934 MILLER CLERK 7782 23-1月 -82 1300 10
7369 SMITH CLERK 7902 17-12月-80 800 20
7876 ADAMS CLERK 7788 13-7月 -87 1100 20
7900 JAMES CLERK 7698 03-12月-81 950 30
已選擇8行。
SQL> spool off