Beruflich Dokumente
Kultur Dokumente
Objectives
After completing this lesson, you should be able to do the
following:
Define subqueries
Describe the types of problems that the subqueries can
solve
List the types of subqueries
Write single-row and multiple-row subqueries
7-2
Lesson Agenda
Multiple-row subqueries
Use ALL or ANY operator.
7-3
7-4
Subquery Syntax
SELECT
FROM
WHERE
7-5
select_list
table
expr operator
(SELECT
FROM
select_list
table);
Using a Subquery
7-6
7-7
Types of Subqueries
Single-row subquery
Main query
Subquery
returns
ST_CLERK
Multiple-row subquery
Main query
Subquery
7-8
returns
ST_CLERK
SA_MAN
Lesson Agenda
Multiple-row subqueries
Use ALL or ANY operator
7-9
Single-Row Subqueries
7 - 10
Meaning
Equal to
>
Greater than
>=
<
Less than
<=
<>
Not equal to
7 - 11
7 - 12
7 - 13
department_id, MIN(salary)
employees
department_id
2500
MIN(salary) >
(SELECT MIN(salary)
FROM
employees
WHERE department_id = 50);
7 - 14
7 - 15
Lesson Agenda
Multiple-row subqueries
Use IN, ALL, or ANY
7 - 16
Multiple-Row Subqueries
7 - 17
IN
ANY
ALL
7 - 18
7 - 19
7 - 20
Lesson Agenda
Multiple-row subqueries
Use ALL or ANY operator
7 - 21
SELECT emp.last_name
FROM
employees emp
WHERE emp.employee_id NOT IN
(SELECT mgr.manager_id
FROM
employees mgr);
7 - 22
Quiz
Using a subquery is equivalent to performing two sequential
queries and using the result of the first query as the search
values in the second query.
1. True
2. False
7 - 24
Summary
In this lesson, you should have learned how to:
Identify when a subquery can help solve a problem
Write subqueries when a query is based on unknown
values
SELECT
FROM
WHERE
7 - 25
select_list
table
expr operator
(SELECT select_list
FROM
table);
Practice 7: Overview
This practice covers the following topics:
Creating subqueries to query values based on unknown
criteria
Using subqueries to find out the values that exist in one set
of data and not in another
7 - 26