今天一個開發問到一個not exists問題,小小做個實驗
實驗1
SQL> select * from emp e1 where not exists (select 1 from emp e2 where e1.mgr = e2.empno);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- ------------------ ---------- ---------- ----------
7839 KING PRESIDENT 17-NOV-81 5000 10
實驗2
SQL> select * from emp e1 where not exists (select 1 from emp e2 where e1.empno = e2.mgr);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- ------------------ ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7934 MILLER CLERK 7782 23-JAN-82 1300 10
8 rows selected.
1、2 SQL大致一樣,出現的結果卻完全不一樣
實驗1是這裡有多少人沒有manager
實驗2是這裡多少人存在manager