Scalar Subqueries in SQL
A query that returns one value, used where a value goes
A scalar subquery is a SELECT inside parentheses that returns a single row and column — one value. Because it IS a value, you can drop it into comparisons, SELECT lists and CASE expressions: WHERE salary > (SELECT AVG(salary) FROM employees). The inner query runs (conceptually) first, its single value substitutes into the outer query, and the outer WHERE evaluates against it.
Write the inner query standalone
SELECT AVG(salary) FROM employees — run it alone first: it must return exactly one row, one column to be a scalar.
Substitute it where a value goes
Wrap in parentheses and place it in a comparison, SELECT list or CASE: WHERE salary > (…).
The engine folds it in
Most plans evaluate the inner query once (an InitPlan), then treat its value as a constant for the outer scan.
The outer query compares against it
Each employee row is tested against the folded constant — above-average filtering with no magic number in the code.
Guard the one-row contract
If the inner query could return zero or many rows, wrap it: MAX(…), MIN(…), or add LIMIT 1 — the query errors otherwise.
1SELECT name, salary2FROM employees3WHERE salary > (SELECT AVG(salary) FROM employees);
| name | salary |
|---|---|
| Alice | 85000 |
| Cynthia | 91000 |
| Farid | 73000 |
The inner query computes the average across all six employees (67,666.67). That single number then acts as a constant in the outer WHERE, keeping only the three higher earners. One query, two layers, zero magic numbers.
Above-average earners
The canonical compare-everyone-to-the-average query.
1SELECT name, salary2FROM employees3WHERE salary > (SELECT AVG(salary) FROM employees);
| name | salary |
|---|---|
| Alice | 85000 |
| Cynthia | 91000 |
| Farid | 73000 |
Scalar subquery in the SELECT list
Annotate each row with a value computed elsewhere.
1SELECT name, salary,2 (SELECT AVG(salary) FROM employees) AS company_avg,3 salary - (SELECT AVG(salary) FROM employees) AS vs_avg4FROM employees WHERE department = 'Sales';
| name | salary | company_avg | vs_avg |
|---|---|---|---|
| Bob | 52000 | 67666.67 | -15666.67 |
| Elena | 58000 | 67666.67 | -9666.67 |
The one-row rule, violated
A scalar spot demands exactly one value.
1SELECT name2FROM employees3WHERE salary = (SELECT salary FROM employees WHERE department = 'Sales');
| error |
|---|
| ERROR: more than one row returned by a subquery used as an expression |
MAX/MIN guards — latest event per entity
A robust pattern that never violates the one-row rule.
1SELECT * FROM orders o2WHERE o.placed_at = (SELECT MAX(placed_at) FROM orders i WHERE i.customer_id = o.customer_id);3-- correlated, but MAX guarantees one value
| order_id | customer | placed_at |
|---|---|---|
| #102 | Elena | 2026-08-30 |
| #101 | Bob | 2026-08-28 |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.