0% found this document useful (0 votes)
19 views11 pages

Subquery

Uploaded by

indu.verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views11 pages

Subquery

Uploaded by

indu.verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

What Is a Subquery?

A
A subquery
subquery is
is aa SELECT
SELECT statement
statement
embedded
embedded inin aa clause
clause of
of another
another SQL
SQL
statement.
statement.

Main SELECT . . .
Query FROM . . .
WHERE . . .
(SELECT . . . Subquery
FROM . . .
WHERE . . .)

6-1 Copyright  Oracle Corporation, 1998. All rights reserved.


Subqueries

SELECT select_list
FROM table
WHERE expr operator (SELECT select_list
FROM table);

•• The
The subquery
subquery (inner
(inner query)
query) executes
executes
once
once before
before the
the main
main query.
query.
•• The
The result
result of
of the
the subquery
subquery is
is used
used by
by
the
the main
main query
query (outer
(outer query).
query).

6-2 Copyright  Oracle Corporation, 1998. All rights reserved.


Using a Subquery

SQL> SELECT ename


2 FROM emp 2975
3 WHERE sal >
4 (SELECT sal
5 FROM emp
6 WHERE empno = 7566);

ENAME
ENAME
----------
----------
KING
KING
FORD
FORD
SCOTT
SCOTT

6-3 Copyright  Oracle Corporation, 1998. All rights reserved.


Correlated Subqueries

SELECT outer1, outer2, ...


FROM table1 alias1
WHERE outer1 operator
(SELECT inner1
FROM table2 alias2
WHERE alias1.outer2 =
alias2.inner1);

The
The subquery
subquery references
references aa column
column from
from
aa table
table in
in the
the parent
parent query.
query.

6-4 Copyright  Oracle Corporation, 1998. All rights reserved.


Using Correlated Subqueries
Find
Find all
all employees
employees who
who make
make more
more than
than
the
the average
average salary
salary in
in their
their department.
department.
SQL> SELECT empno, sal, deptno Each time the outer query
2 FROM emp outer is processed the
3 WHERE sal > (SELECT AVG(sal) inner query is
4 FROM emp inner evaluated.
5 WHERE outer.deptno = inner.deptno);

EMPNO
EMPNO SAL
SAL DEPTNO
DEPTNO
--------
-------- ---------
--------- ---------
---------
7839
7839 5000
5000 10
10
7698
7698 2850
2850 30
30
7566
7566 2975
2975 20
20
...
...
66 rows
rows selected.
selected.
6-5 Copyright  Oracle Corporation, 1998. All rights reserved.
Using the EXISTS Operator
•• If
If aa subquery
subquery row
row value
value is
is found:
found:
–– The
The search
search does
does not
not continue
continue inin the
the
inner
inner query.
query.
–– The
The condition
condition is
is flagged
flagged TRUE.
TRUE.
•• If
If aa subquery
subquery row
row value
value is
is not
not found:
found:
–– The
The condition
condition is
is flagged
flagged FALSE.
FALSE.
–– The
The search
search continues
continues in
in the
the inner
inner
query.
query.

6-6 Copyright  Oracle Corporation, 1998. All rights reserved.


Using the EXISTS Operator
Find employees who have at least one
person reporting to them.
SQL> SELECT empno, ename, job, deptno
2 FROM emp outer
3 WHERE EXISTS (SELECT empno
4 FROM emp inner
5 WHERE inner.mgr = outer.empno);

EMPNO ENAME JOB DEPTNO


--------- ---------- --------- ---------
7839 KING PRESIDENT 10
7698 BLAKE MANAGER 30
7782 CLARK MANAGER 10
7566 JONES MANAGER 20
...
6 rows selected.

6-7 Copyright  Oracle Corporation, 1998. All rights reserved.


Using the NOT EXISTS Operator
Find all departments that do not have any
employees.
SQL> SELECT deptno, dname
2 FROM dept d
3 WHERE NOT EXISTS (SELECT '1'
4 FROM emp e
5 WHERE d.deptno = e.deptno);

DEPTNO
DEPTNO DNAME
DNAME
---------
--------- ----------
----------
40
40 OPERATIONS
OPERATIONS

6-8 Copyright  Oracle Corporation, 1998. All rights reserved.


UPDATE

UPDATE
UPDATE table1
table1 alias1
alias1
SET
SET column
column == (SELECT
(SELECT expression
expression
FROM
FROM table2
table2 alias2
alias2
WHERE
WHERE alias1.column
alias1.column == alias2.column);
alias2.column);

Use
Use aa subquery
subquery toto update
update rows
rows in
in one
one table
table
based
based onon rows
rows from
from another
another table.
table.

6-9 Copyright  Oracle Corporation, 1998. All rights reserved.


DELETE

DELETE
DELETE FROM
FROM table1
table1 alias1
alias1
WHERE
WHERE column
column operator
operator
(SELECT
(SELECT expression
expression
FROM
FROM table2
table2 alias2
alias2
WHERE
WHERE alias1.column
alias1.column == alias2.column);
alias2.column);

Use
Use aa subquery
subquery toto delete
delete only
only those
those rows
rows
that
that also
also exist
exist in
in another
another table.
table.

6-10 Copyright  Oracle Corporation, 1998. All rights reserved.


Summary
•• Correlated
Correlated subqueries
subqueries are
are useful
useful
whenever
whenever aa subquery
subquery must
must return
return aa
different
different result
result for
for each
each candidate
candidate row.
row.
•• The
The EXISTS
EXISTS operator
operator is
is aa Boolean
Boolean
operator,
operator, testing
testing the
the presence
presence of of aa
value.
value.
•• Correlated
Correlated subqueries
subqueries can
can be
be used
used with
with
SELECT,
SELECT, UPDATE,
UPDATE, andand DELETE
DELETE
statements.
statements.

6-11 Copyright  Oracle Corporation, 1998. All rights reserved.

You might also like