Before you start, you need
Summarise data with COUNT, SUM, AVG, MIN and MAX · Create per-group summaries with GROUP BY · Break queries into steps with CTEs
Use a query as if it were a table
You want to know how much each customer spends on average. First you have to add up each customer's orders, then average those sums. But AVG(SUM(total)) is not allowed: one aggregate cannot sit inside another. You need two steps: calculate the summary and then work with it.
A subquery in FROM turns that first result into a temporary table. The outer query reads it as if it were a table: SELECT AVG(customer_total) FROM (SELECT customer_id, SUM(total) AS customer_total FROM orders GROUP BY customer_id) AS summary.
The alias is the name of that temporary table (AS summary). PostgreSQL and MySQL require it and SQLite lets you leave it out; always write it. The outer query can only use the columns the inner one produces (customer_id, customer_total), so give them clear aliases.
It does the same job as a CTE, but written inline. Use a CTE if the step is long or you need it several times; use a subquery in FROM when it is a short, one-off step. To understand it, run the inner query on its own first and look at its rows.
SELECT ...
FROM (
SELECT ... FROM ... GROUP BY ...
) AS summary;See it in action
How much does each customer who has placed orders spend on average?
SELECT ROUND(AVG(customer_total), 2) AS average_per_customer
FROM (
SELECT customer_id, SUM(total) AS customer_total
FROM orders
GROUP BY customer_id
) AS summary;| average_per_customer |
|---|
| 393.19 |
- The inner query adds up each customer's orders: you get one row per customer with their total. You can run it on its own to see this.
AS summaryturns that result into a temporary table with the columns customer_id and customer_total.- The outer query calculates the average of customer_total. The average of the per-customer sums is not the same as the average of the individual orders.
- It all exists only during this statement: no new table is stored.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Average a summary
Calculate the average of the per-customer sums. Add up each customer's orders in a subquery inside FROM and return the average, rounded to two decimal places, with the alias average_per_customer.
Expected result: 1 row with the column average_per_customer, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
You need two steps: a sum per customer and then the average of those sums.
Write the per-customer sum in parentheses after FROM and give it an alias, for example summary.
Structure: SELECT ROUND(AVG(customer_total), 2) AS average_per_customer FROM (SELECT customer_id, SUM(total) AS customer_total FROM orders GROUP BY customer_id) AS summary.
Solution explained
SELECT ROUND(AVG(customer_total), 2) AS average_per_customer FROM (SELECT customer_id, SUM(total) AS customer_total FROM orders GROUP BY customer_id) AS summary;The subquery leaves one row per customer with their sum; the outer query averages those sums. Each customer counts once, unlike averaging the individual orders.
Filter a summary without HAVING
Guided practice
Filter a summary without HAVING
Return customer_id and customer_total for customers whose orders add up to more than 300. Calculate the sum with ROUND(SUM(total), 2) in a subquery inside FROM and filter in the outer query with WHERE, without using HAVING. Sort by customer_total descending.
Expected result: 4 rows with the columns customer_id, customer_total, in that order. Keep the row order requested.
View tables and data
Open this panel to see the full data.
First get one row per customer with their sum; then filter that result.
The subquery calculates customer_total; the outer query's WHERE compares customer_total with 300.
Structure: SELECT customer_id, customer_total FROM (...) AS summary WHERE customer_total > 300 ORDER BY customer_total DESC.
Solution explained
SELECT customer_id, customer_total FROM (SELECT customer_id, ROUND(SUM(total), 2) AS customer_total FROM orders GROUP BY customer_id) AS summary WHERE customer_total > 300 ORDER BY customer_total DESC;Once the summary is a table, you can filter it with WHERE like any other. The result is the same as with HAVING, but now the summary can be reused for further calculations.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Count groups
Count how many categories have at least 10 units of stock in total. First calculate SUM(stock) AS units per category in a subquery inside FROM, then return a categories column with the count.
Expected result: 1 row with the column categories, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
Count categories, not products: first you need one row per category.
The subquery adds up the stock per category; the outer query filters units >= 10 and counts the remaining rows.
Structure: SELECT COUNT(*) AS categories FROM (SELECT category, SUM(stock) AS units FROM products GROUP BY category) AS summary WHERE units >= 10.
Solution explained
SELECT COUNT(*) AS categories FROM (SELECT category, SUM(stock) AS units FROM products GROUP BY category) AS summary WHERE units >= 10;To count groups you first have to form them: the subquery leaves one row per category, and the outer query counts the ones that reach the threshold.
Key takeaways
- A subquery in FROM behaves like a temporary table.
- Always give it an alias, and use its columns from the outer query.
- It lets you aggregate over a summary that is already aggregated.
- A CTE does the same job; choose whichever reads better.
Go deeper
A subquery in FROM can also be joined to other tables with JOIN, as long as it has an alias. For long or reused steps, a CTE is usually easier to read.