Loading…
A subquery is a query inside another query. It's like asking a question within a question.
Sometimes you need to use the result of one query as part of another:
SELECT name, price
FROM products
WHERE price > (SELECT AVG(price) FROM products);
How it works:
(SELECT AVG(price) FROM products) runs first and returns a number (e.g., 45.5)WHERE price > 45.5| Type | Returns | Used with |
|---|---|---|
| Scalar | One value | <code>=</code>, <code>></code>, <code><</code> |
| List | Multiple values | <code>IN</code>, <code>NOT IN</code> |
| Table | Multiple rows & columns | <code>FROM</code> |
Don't forget the parentheses around subqueries!
Find all products with a price higher than the average price.
Downloading SQL engine… (one-time)
This runs entirely in your browser and is cached for next time.