Chapter 8. Subqueries

To this point, I’ve used a single SELECT statement to retrieve data from one or more tables. In this chapter, I’ll describe nested queries, which allow you to retrieve or modify data based on another query’s result.

A subquery is a SELECT statement embedded in another SQL statement. You can nest a subquery in:

  • The SELECT, FROM, WHERE, or HAVING clause of another SELECT statement

  • Another subquery

  • An INSERT, UPDATE, or DELETE statement

In general, you can use a subquery anywhere an expression is allowed, but your DBMS may place specific restrictions on where subqueries may appear. I’ll cover subqueries nested in a SELECT statement or another subquery in this chapter, and subqueries embedded in INSERT, UPDATE, and DELETE statements ...

Get SQL: Visual QuickStart Guide 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.