Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 15

6

Writing Correlated
Subqueries

Copyright

Oracle Corporation, 1998. All rights reserved.

Objectives
After completing this lesson, you should be
able to do the following:
Describe the types of problems that can
be solved with correlated subqueries
Write correlated subqueries
Use the EXISTS and NOT EXISTS
operators
Update and delete rows using correlated
subqueries
6-2

Copyright

Oracle Corporation, 1998. All rights reserved.

What Is a Subquery?
A subquery is a SELECT statement
embedded in a clause of another SQL
statement.
Main
Query

6-3

SELECT . . .
FROM . . .
WHERE . . .

Copyright

(SELECT . . .
FROM . . .
WHERE . . .)

Oracle Corporation, 1998. All rights reserved.

Subquery

Subqueries
The subquery (inner query) executes once before the main query.

SELECT
FROM
WHERE

select_list
table
expr operator (SELECT
FROM

The result of the subquery is used by the main query (outer query).

6-4

Copyright

select_list
table);

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-5

Copyright

Oracle Corporation, 1998. All rights reserved.

Correlated Subqueries
Used to affect row-by-row processing,
each subquery is executed once for every
row of the outer query.
GET
candidate row

EXECUTE
inner query using candidate row value

USE
value(s) from inner query to qualify
candidate row
6-6

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 subquery references a column from


a table in the parent query.

6-7

Copyright

Oracle Corporation, 1998. All rights reserved.

Using Correlated Subqueries


Find all employees who make more than
the average salary in their department.
SQL> SELECT empno, sal, deptno
Each time the outer query
is processed the
2 FROM
emp outer
inner query is
3 WHERE sal > (SELECT AVG(sal)
evaluated.
4
FROM
emp inner
5
WHERE outer.deptno = inner.deptno);
EMPNO
SAL
DEPTNO
EMPNO
SAL
DEPTNO
--------------- ----------------- ----------------7839
5000
10
7839
5000
10
7698
2850
30
7698
2850
30
7566
2975
20
7566
2975
20
...
...
66 rows
rows selected.
selected.
6-8

Copyright

Oracle Corporation, 1998. All rights reserved.

Using the EXISTS Operator


If a subquery row value is found:
The search does not continue in the

inner query.
The condition is flagged TRUE.

If a subquery row value is not found:


The condition is flagged FALSE.
The search continues in the inner

query.
6-9

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
--------- ---------7839 KING
7698 BLAKE
7782 CLARK
7566 JONES
...
6 rows selected.
6-10

Copyright

JOB
DEPTNO
--------- --------PRESIDENT
10
MANAGER
30
MANAGER
10
MANAGER
20

Oracle Corporation, 1998. All rights reserved.

Using the NOT EXISTS Operator


Find all departments that do not have any
employees.
SQL> SELECT
2 FROM
3 WHERE
4
5

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

6-11

deptno, dname
dept d
NOT EXISTS (SELECT '1'
FROM
emp e
WHERE d.deptno = e.deptno);

DNAME
DNAME
------------------OPERATIONS
OPERATIONS

Copyright

Oracle Corporation, 1998. All rights reserved.

Correlated UPDATE
UPDATE
UPDATE
SET
SET

table1
table1
column
column

alias1
alias1
== (SELECT
(SELECT
FROM
FROM
WHERE
WHERE

expression
expression
table2
table2 alias2
alias2
alias1.column
alias1.column == alias2.column);
alias2.column);

Use a correlated subquery to update rows


in one table based on rows from another
table.

6-12

Copyright

Oracle Corporation, 1998. All rights reserved.

Correlated DELETE
DELETE
DELETE
WHERE
WHERE

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

Use a correlated subquery to delete only


those rows that also exist in another
table.

6-13

Copyright

Oracle Corporation, 1998. All rights reserved.

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

Copyright

Oracle Corporation, 1998. All rights reserved.

Practice Overview
Writing correlated subqueries
Using the EXISTS operator

6-15

Copyright

Oracle Corporation, 1998. All rights reserved.

You might also like