What is Subquery?

A subquery is a query nested inside another query, used to compute a value or a set of rows that the outer query then uses.

Subquery explained

It can appear in the WHERE clause (customers whose spend is above the average), in the FROM clause as a temporary table, or in the SELECT list as a single value.

Subqueries are standard but can get hard to read when nested deeply; a CTE usually expresses the same logic top to bottom.

Example

SELECT customers WHERE total spend is greater than (SELECT the average spend) returns customers who spend more than average.

Frequently asked questions

What is Subquery?
A subquery is a query nested inside another query, used to compute a value or a set of rows that the outer query then uses.
Can you give an example of Subquery?
SELECT customers WHERE total spend is greater than (SELECT the average spend) returns customers who spend more than average.