Databases & SQL · reviewed in August 2026
Subquery
A subquery is a SQL query nested inside another, used as if it were a value, a list, or a temporary table within the main query. It can appear in the SELECT, FROM, or WHERE clause, depending on what it needs to return.
SELECT name FROM customers WHERE id IN ( SELECT customer_id FROM orders WHERE total > 1000 );
Frequently asked questions
What is a correlated subquery?
It's a subquery that references a column from the outer query, so it re-runs once per row of the main query — more flexible but potentially slower than an independent subquery.
When is a subquery better than a JOIN?
When you only need to check existence (with EXISTS or IN) or a single aggregated value, without pulling extra columns from the other table; to combine and display data from both tables, a JOIN is usually clearer and more efficient.
Can a subquery go in the FROM clause?
Yes — it behaves like a temporary table with its own alias, useful for pre-filtering or pre-aggregating data before the outer query operates on that result.