In short
"Which orders are above the average order?" needs the average first, then the filter. A subquery lets you do both in one query, like asking a question inside a question.
A subquery is a query inside another query, used where a value, list or table is expected. It lets you compute something first, such as the average order value, and then filter by it.

Think of it like this
A gift box inside a gift box
You open a big box and find a small box inside. You must open the small one first to get what's needed for the big one. A subquery is the inner box: the database runs it first and hands its answer to the outer query.
Watch it happen
Orders above the average
need the average
1/5The outer query can't filter until it knows the average amount.
The big ideas
What you need to know
Scalar subquery
Returns one value, like an average, and can sit wherever a number would go.
IN subquery
WHERE customer_id IN (SELECT ...) filters against a list returned by the inner query.
Subquery in FROM
A subquery can act as a temporary table that the outer query selects from. It needs an alias.
Correlated subquery
Refers to the outer row, so it runs once per row. Powerful but can be slow on big tables.
See it in code
Walk through the code
1SELECT order_id, customer_id, amount2FROM orders3WHERE amount > (SELECT AVG(amount) FROM orders);4 5SELECT name6FROM customers7WHERE customer_id IN (8 SELECT customer_id FROM orders WHERE amount > 50009);1/2The bracketed query returns the average. The outer WHERE compares every order to it.
Two common shapes: compare against one computed value, and filter against a list.
Save yourself the pain
Common mistakes
Oops
Using = with a subquery that can return several rows.
Do this instead
Use IN for lists. = only works when the subquery returns exactly one value.
Oops
Nesting four subqueries deep until nobody can read it.
Do this instead
Once it gets deep, switch to CTEs (WITH) so each step has a name.
Your mission
Questions inside questions
Quick check
What does WHERE amount > (SELECT AVG(amount) FROM orders) do?
Go deeper
- 1Find orders larger than the overall average order value using a subquery in WHERE.
- 2Write a subquery that returns a list and use it with IN.
- 3Run the inner query on its own first to check what it returns.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.