SQL Scalar Subqueries
What you will learn
- What a subquery is and what a scalar subquery returns
- How to compare each row with an average or a maximum
- How to use a subquery in
SELECT - What happens with no row or several rows
This lesson runs on the sample database you chose in lesson 1.1 (use the Example data picker at the top). Run the cells from top to bottom.
1. A query inside a query
A subquery is a SELECT written inside another statement, in brackets. The database runs the inner query first and uses its result. A scalar subquery returns exactly one value (one row, one column), so it can be used wherever a single value fits:
SELECT (SELECT 40 + 2) AS answer;
answer
------
42
(1 row)
SELECT (SELECT 40 + 2) AS answer;
answer
------
42
(1 row)
SELECT (SELECT 40 + 2) AS answer;
answer
------
42
(1 row)
That is a toy. The useful case is a value you cannot know in advance, such as an average.
2. Compare each row with an average
"Which products cost more than the average product?" You need the average first, then the comparison. The subquery does both in one statement:
SELECT name, price
FROM products
WHERE price > (SELECT AVG(price) FROM products)
ORDER BY price DESC
LIMIT 5;
name price
------------ -----
Water bottle 86.9
Tea kettle 79.0
Highlighters 79.0
Foam roller 79.0
Drawing kit 53.9
(5 rows)
SELECT account_id, balance_cents
FROM accounts
WHERE balance_cents > (SELECT AVG(balance_cents) FROM accounts)
ORDER BY balance_cents DESC
LIMIT 5;
account_id balance_cents
---------- -------------
398 502080
243 432800
165 428004
238 401600
295 379900
(5 rows)
SELECT course_id, code, credits
FROM courses
WHERE credits > (SELECT AVG(credits) FROM courses)
ORDER BY credits DESC, course_id
LIMIT 5;
course_id code credits
--------- ------- -------
1 MATH101 4
3 MATH301 4
4 SCI101 4
5 SCI201 4
6 SCI202 4
(5 rows)
You cannot write WHERE price > AVG(price): an aggregate is not allowed in WHERE (lesson 3.3). The subquery is the way around it.
3. The newest or the biggest row
A subquery with MAX or MIN finds the row at the top or bottom, and keeps ties:
SELECT order_id, order_date FROM orders
WHERE order_date = (SELECT MAX(order_date) FROM orders)
ORDER BY order_id;
order_id order_date
-------- ----------
17 2026-09-30
114 2026-09-30
285 2026-09-30
543 2026-09-30
1330 2026-09-30
1953 2026-09-30
(6 rows)
SELECT account_id, opened_on FROM accounts
WHERE opened_on = (SELECT MAX(opened_on) FROM accounts)
ORDER BY account_id;
account_id opened_on
---------- ----------
269 2026-07-26
(1 row)
SELECT student_id, first_name, birth_date FROM students
WHERE birth_date = (SELECT MIN(birth_date) FROM students)
ORDER BY student_id;
student_id first_name birth_date
---------- ---------- ----------
280 Quinn 2007-01-05
(1 row)
ORDER BY ... LIMIT 1 would return one row only; the subquery returns every row that ties.
4. A subquery in SELECT
A scalar subquery can also sit in the select list, to show a reference value beside every row:
SELECT name, price,
ROUND(price - (SELECT AVG(price) FROM products), 2) AS above_average
FROM products
ORDER BY above_average DESC
LIMIT 4;
name price above_average
------------ ----- -------------
Water bottle 86.9 57.74
Tea kettle 79.0 49.84
Highlighters 79.0 49.84
Foam roller 79.0 49.84
(4 rows)
SELECT account_id, balance_cents,
balance_cents - (SELECT CAST(AVG(balance_cents) AS INTEGER) FROM accounts) AS above_average
FROM accounts
ORDER BY above_average DESC
LIMIT 4;
account_id balance_cents above_average
---------- ------------- -------------
398 502080 363860
243 432800 294580
165 428004 289784
238 401600 263380
(4 rows)
SELECT code, credits,
credits - (SELECT MIN(credits) FROM courses) AS above_minimum
FROM courses
ORDER BY above_minimum DESC, code
LIMIT 4;
code credits above_minimum
------- ------- -------------
CS201 4 2
CS301 4 2
MATH101 4 2
MATH301 4 2
(4 rows)
5. No row, or too many rows
- If the subquery returns no row, its value is
NULL, and a comparison withNULLis never true (lesson 2.5). - If it returns several rows, SQLite uses the first one without warning. Some other databases may raise an error instead. Do not rely on either: make sure the subquery returns one row.
SELECT (SELECT 1 WHERE 1 = 0) AS no_row;
no_row
------
NULL
(1 row)
SELECT (SELECT 1 WHERE 1 = 0) AS no_row;
no_row
------
NULL
(1 row)
SELECT (SELECT 1 WHERE 1 = 0) AS no_row;
no_row
------
NULL
(1 row)
6. Subquery or join?
For a single reference value, a subquery is natural. When you need columns from another table next to each row, a join is clearer (chapter 4). The two often give the same result; choose the one that reads best.
Worked example: above-average rows
| Question | Idea |
|---|---|
| "Products above the average price" | WHERE price > (SELECT AVG(price) FROM products) |
| "The most recent order" | WHERE order_date = (SELECT MAX(order_date) FROM orders) |
| "How far each price is from the average" | the subquery in the select list |
Always run the inner query alone first, and check it returns one value. The tables are from the Shop sample.
Common mistakes
- A subquery that returns several rows where one value is expected.
- Forgetting the brackets.
- Using
=with a subquery that can return no row, getting no results. - Repeating a heavy subquery in many places (lesson 7.5 shows
WITH).
Key takeaways
- A subquery is a
SELECTin brackets inside another statement. - A scalar subquery returns one value and works wherever a value fits.
- It lets
WHEREcompare with an average or a maximum, which an aggregate inWHEREcannot. - Make sure it returns exactly one row.
Practice
1. Write the WHERE that keeps products above the average price.
Show answer
WHERE price > (SELECT AVG(price) FROM products)
2. Why not write WHERE price > AVG(price)?
Show answer
Aggregates are not allowed in WHERE; the rows are not yet grouped.
3. What does a scalar subquery return when it finds no row?
Show answer
NULL.
4. Why is = (SELECT MAX(...)) better than ORDER BY ... LIMIT 1 for ties?
Show answer
It returns every row that has the maximum value, not just one.
5. What does SQLite do if the subquery returns several rows?
Show answer
It uses the first row without a warning, so you must make sure it returns one row.
6. When is a join clearer than a subquery?
Show answer
When you need columns from the other table next to each row.