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
| name | price |
|---|---|
| Standing desk | 549.0 |
| Ergonomic chair | 399.0 |
| 27-inch monitor | 329.0 |
| Noise-cancelling headphones | 249.0 |
4 rows
Related
Learn it step by step in the free lesson Subqueries & Set Operations, with exercises that check your queries.