Professional Documents
Culture Documents
Subqueries
Objectives
After
After completing
completing this
this lesson,
lesson, you
you should
should
be
be able
able to
to do
do the
the following:
following:
•• Describe
Describe the
the types
types of
of problems
problems that
that
subqueries
subqueries can
can solve
solve
•• Define
Define subqueries
subqueries
•• List
List the
the types
types of
of subqueries
subqueries
•• Write
Write single-row
single-row and
and multiple-row
multiple-row
subqueries
subqueries
6-2
Using a Subquery
to Solve a Problem
““Who
Who has
has aa salary
salary greater
greater than
than Jones’?”
Jones’?”
Main Query
Subquery
?
“What is Jones’ salary?”
6-3
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 isis used
used by
by the
the main
main query
query (outer
(outer query).
query).
6-4
Using a Subquery
SQL> SELECT ename
2 FROM emp 2975
3 WHERE sal >
4 (SELECT sal
5 FROM emp
6 WHERE empno=7566);
ENAME
----------
KING
FORD
SCOTT
6-5
Guidelines for Using Subqueries
•• Enclose
Enclose subqueries
subqueries in
in parentheses.
parentheses.
•• Place
Place subqueries
subqueries on
on the
the right
right side
side of
of the
the
comparison
comparison operator.
operator.
•• Do
Do not
not add
add an
an ORDER
ORDER BYBY clause
clause to
to aa
subquery.
subquery.
•• Use
Use single-row
single-row operators
operators with
with single-
single-
row
row subqueries.
subqueries.
•• Use
Use multiple-row
multiple-row operators
operators with
with
multiple-row
multiple-row subqueries.
subqueries.
6-6
Types of Subqueries
•• Single-row
Single-row subquery
subquery
Main query
returns
Subquery CLERK
•• Multiple-row
Multiple-row subquery
subquery
Main query
Subquery
returns CLERK
MANAGER
•• Multiple-column
Multiple-column subquery
subquery
Main query
returns
Subquery CLERK 7900
MANAGER 7698
6-7
Single-Row Subqueries
•• Return
Return only
only one
one row
row
•• Use
Use single-row
single-row comparison
comparison operators
operators
Operator Meaning
= Equal to
6-8
Executing Single-Row Subqueries
SQL> SELECT ename, job
2 FROM emp
3 WHERE job = CLERK
4 (SELECT job
5 FROM emp
6 WHERE empno = 7369)
7 AND sal > 1100
8 (SELECT sal
9 FROM emp
10 WHERE empno = 7876);
ENAME JOB
---------- ---------
MILLER CLERK
6-9
Using Group Functions
in a Subquery
SQL> SELECT ename, job, sal
800
2 FROM emp
3 WHERE sal =
4 (SELECT MIN(sal)
5 FROM emp);
6-10
HAVING Clause with Subqueries
•• The
The Oracle
Oracle Server
Server executes
executes subqueries
subqueries
first.
first.
•• The
The Oracle
Oracle Server
Server returns
returns results
results into
into
the
the HAVING
HAVING clause
clause of
of the
the main
main query.
query.
SQL> SELECT deptno, MIN(sal)
2 FROM emp
3 GROUP BY deptno
800
4 HAVING MIN(sal) >
5 (SELECT MIN(sal)
6 FROM emp
7 WHERE deptno = 20);
6-11
What Is Wrong
with This Statement?
SQL> SELECT empno, ename
2 FROM emp
3 WHERE sal =
4
w i t h(SELECT MIN(sal)
5 r at or FROM emp
6 ow op
e er y GROUP BY
-r q u deptno);
ng l e w sub
Si l e- ro
u l t ip
m
ERROR:
ORA-01427: single-row subquery returns more than
one row
no rows selected
6-12
Will This Statement Work?
6-13
Multiple-Row Subqueries
•• Return
Return more
more than
than one
one row
row
•• Use
Use multiple-row
multiple-row comparison
comparison operators
operators
Operator Meaning
6-14
Using ANY Operator
in Multiple-Row Subqueries
SQL> SELECT empno, ename, job 1300
2 FROM emp 1100
800
3 WHERE sal < ANY 950
4 (SELECT sal
5 FROM emp
6 WHERE job = 'CLERK')
7 AND job <> 'CLERK';
6-15
Using ALL Operator
in Multiple-Row Subqueries
SQL> SELECT empno, ename, job 1566.6667
2 FROM emp 2175
2916.6667
3 WHERE sal > ALL
4 (SELECT avg(sal)
5 FROM emp
6 GROUP BY deptno);
6-16
Summary
Subqueries
Subqueries are
are useful
useful when
when aa query
query is
is
based
based on
on unknown
unknown values.
values.
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);
6-17
Practice Overview
6-18