Previous Topic: ScenariosNext Topic: SELECT Statement in a WHERE Clause


Nesting SELECT Statements

Goal

When you have completed this chapter, you will be able to nest a SELECT statement within another SELECT statement to retrieve specified data.

Summary

An SQL request that is nested inside another SELECT statement is called a subquery. The subquery returns a set of values for use in the outer SELECT statement:

select
from
where column =  ◄───────────┐
                            │
               (select  ────┘
               from
               where column =  ◄───────────┐
                                           │
                              (select  ────┘
                              from
                              where column = ));

You nest SELECT statements when you want to use data from one table as part of the criteria of another table. A subquery is often used in conjunction with predicates IN and EXISTS.

This section contains the following topics:

SELECT Statement in a WHERE Clause

Using a Subquery with IN

Using an Aggregate Function in a Nested SELECT Statement

Using EXISTS

Things to Remember about Subqueries