Professional Documents
Culture Documents
Les 06
Les 06
Objectives
6-2
Using a Subquery
to Solve a Problem
Main Query:
Subquery
?
What is Abel’s salary?
6-3
Subquery Syntax
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);
• The subquery (inner query) executes once before the main query.
• The result of the subquery is used by the main query (outer
query).
6-4
Using a Subquery
SELECT last_name
FROM employees 11000
WHERE salary >
(SELECT salary
FROM employees
WHERE last_name = 'Abel');
6-5
Guidelines for Using Subqueries
6-6
Types of Subqueries
• Single-row subquery
Main query
returns
Subquery ST_CLERK
• Multiple-row subquery
Main query
returns ST_CLERK
Subquery
SA_MAN
6-7
Single-Row Subqueries
= Equal to
6-8
Executing Single-Row Subqueries
6-9
Using Group Functions in a Subquery
6-10
The HAVING Clause with Subqueries
6-11
What is Wrong
with this Statement?
ERROR
ERROR at
at line
line 4:
4:
ORA-01427:
ORA-01427: single-row
single-row subquery
subquery returns
returns more
more than
than
one
one row
row
6-12
Will this Statement Return Rows?
no
no rows
rows selected
selected
Subqu
Subque ry returns no values
ery
6-13
Multiple-Row Subqueries
6-14
Using the ANY Operator
in Multiple-Row Subqueries
6-15
Using the ALL Operator
in Multiple-Row Subqueries
6-16
Null Values in a Subquery
SELECT emp.last_name
FROM employees emp
WHERE emp.employee_id NOT IN
(SELECT mgr.manager_id
FROM employees mgr);
no rows selected
6-17
Practice 6 Overview
6-18