Lab 07
Lab 07
Lesson Agenda
7-2
Using a Subquery to Solve a Problem
Main query:
Subquery:
7-3
Subquery Syntax
SELECT select_list
FROM table
WHERE expr
operator (SELECT select_list
FROM table);
7-4
Using a Subquery
7-5
Guidelines for Using Subqueries
7-6
Types of Subqueries
• Single-row subquery
Main query
returns
Subquery ST_CLERK
• Multiple-row subquery
Main query
returns ST_CLERK
Subquery
SA_MAN
7-7
Lesson Agenda
7-8
Single-Row Subqueries
Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to
7-9
Executing Single-Row Subqueries
j
o
b
_
i
d
7 - 10
Using Group Functions in a Subquery
7 - 11
The HAVING Clause with
Subqueries
• The Oracle server executes the subqueries first.
• The Oracle server returns results into the HAVING
clause of the main query.
SELECT department_id, MIN(salary)
FROM employees
GROUP BY department_id 2500
HAVING MIN(salary) >
(SELECT MIN(salary)
FROM employees
WHERE department_id = 50);
7 - 12
What Is Wrong with This Statement?
Single-row operator
with multiple-row
subquery
7 - 13
No Rows Returned by the Inner Query
7 - 14
Lesson Agenda
7 - 15
Multiple-Row Subqueries
7 - 17
Using the ALL Operator
in Multiple-Row Subqueries
7 - 18
Lesson Agenda
7 - 19
Null Values in a Subquery
SELECT emp.last_name
FROM employees emp
WHERE emp.employee_id NOT IN
(S
ELFROM employees mgr);
EC
T
mg
r.
ma
na
ge
r_
id
7 - 20
Quiz
7 - 23
Thank You