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
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 ...