Introduction to subqueries
A subquery is a query nested within another query, where the inner query is executed first and its result is used as input to the outer query. In other words, a subquery is a query within a query. Subqueries can be used to return data that is used in the main query as a condition to further restrict the data that is retrieved.
A subquery is a SELECT
statement that is nested inside another SELECT
, INSERT
, UPDATE
, or DELETE
statement. The result of the subquery is used to modify the data retrieved by the main query.
Subqueries are used to retrieve data from multiple tables based on complex conditions and to aggregate and summarize data in a way that cannot be done with a single query.
Figure 7.1 – Subquery
The syntax is as follows:
SELECT column_listFROM table_name WHERE column_name operator (SELECT column_name ...