What Is a Subquery?

A subquery is an additional method for handling multitable manipulations. It is a SELECT statement that nests

  • Inside the WHERE or HAVING (and in some systems, SELECT or FROM) clause of another SELECT statement;

  • Inside an INSERT, UPDATE, or DELETE statement; or

  • Inside another subquery.

The ability to nest SQL statements is the reason that SQL was originally called the Structured Query Language. The term subquery is sometimes used to refer to an entire set of statements that includes one or more subqueries, as well as to an individual nestling. Each enclosing statement, the next level up in a subquery, is the outer level for its inner subquery.

Subqueries are a complex topic because there are two processing types—simple subqueries ...

Get Practical SQL Handbook, The: Using SQL Variants, Fourth 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.