Embedding a Subquery Within a Subquery

A subquery can be embedded within another subquery, just as you can embed the subquery within a regular query. When a subquery is used, that subquery is resolved before the main query. Likewise, the lowest level subquery is resolved first in embedded or nested subqueries, working out to the main query.

Note

You must check your particular implementation for limits on the number of subqueries, if any, that can be used in a single statement. It may differ between vendors.

The basic syntax for embedded subqueries is as follows:

 SELECT COLUMN_NAME [, COLUMN_NAME ] FROM TABLE1 [, TABLE2 ] WHERE COLUMN_NAME OPERATOR (SELECT COLUMN_NAME FROM TABLE WHERE COLUMN_NAME OPERATOR (SELECT COLUMN_NAME FROM TABLE [ WHERE ...

Get Sams Teach Yourself SQL in 24 Hours, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.