Monday, October 12, 2009

use of exists and not exists in sql

SQL> conn scott/scott
Connected.
SQL> set timing on
SQL> SELECT * FROM ( SELECT DEPTNO FROM DEPT) D;

DEPTNO
----------
10
20
30
40

Elapsed: 00:00:00.03

SQL>
1 SELECT * FROM ( SELECT DEPTNO FROM DEPT) D
2 WHERE EXISTS ( SELECT NULL
3 FROM EMP E
4* WHERE E.DEPTNO = D.DEPTNO)
5 /

DEPTNO
----------
10
20
30

Elapsed: 00:00:00.00
SQL>


SQL>
1 SELECT * FROM ( SELECT DEPTNO FROM DEPT) D
2 WHERE NOT EXISTS ( SELECT NULL
3 FROM EMP E
4* WHERE E.DEPTNO = D.DEPTNO)
5 /

DEPTNO
----------
40

Elapsed: 00:00:00.03
SQL>

No comments: