Home / SQL / Reference / Subqueries, CTEs and sets

SQL Subquery

A query inside another query.

Syntax

(SELECT …)

Returns one value (used with = or as a column), a list (used with IN) or a table (used in FROM, with an alias).

Example

Query (on the shop database used in the SQL course)

SELECT name, price
FROM products
WHERE price > (SELECT AVG(price) FROM products)
ORDER BY price DESC;

Result

nameprice
Standing desk549.0
Ergonomic chair399.0
27-inch monitor329.0
Noise-cancelling headphones249.0

4 rows

Related

Learn it step by step in the free lesson Subqueries & Set Operations, with exercises that check your queries.