Module 5 · Advanced queries

Turn a summary into a table with a subquery in FROM

By the end you will be able to use the result of a query as if it were a table.

About 14 min

0 of 52 lessons

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.

Minimal syntax
SELECT ...
FROM (
  SELECT ... FROM ... GROUP BY ...
) AS summary;

See it in action

How much does each customer who has placed orders spend on average?

SQL
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;
Example result: 1 rows
average_per_customer
393.19
  1. 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.
  2. AS summary turns that result into a temporary table with the columns customer_id and customer_total.
  3. 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.
  4. 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

Not started

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.

Filter a summary without HAVING

Guided practice

Filter a summary without HAVING

Not started

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.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Count groups

Not started

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.

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.

Next: Practice: choose between subqueries and CTEs

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.