Stop 07 · Subqueries, CTEs & Set OperationsTopics · 5/9

Derived Tables (Subqueries in FROM)

Part of the SQL Roadmap

In short

"What's the average spend per customer?" needs two steps: total per customer, then average those totals. A derived table lets you do step one inside FROM and step two outside it.

A derived table is a subquery in the FROM clause that acts like a temporary table, often used to aggregate first and then filter or join.

The jaadu sits at an exam desk, scribbling sums in a narrow folded margin of an answer sheet with one pencil, then neatly copying a single result into the main box with another.

Think of it like this

Rough work in the exam margin

In a maths board exam, you work out the messy intermediate numbers in the rough-work column first. Then you use those results for the final answer on the main page. A derived table is the rough work: a small result calculated inside FROM, which the outer query then treats like a normal table.

Watch it happen

Aggregate inside, then aggregate again

orders
inner GROUP BY
derived table t
outer AVG

1/45,000 delivered orders from 1,200 customers.

The big ideas

What you need to know

A query as a table

Anything you can SELECT can sit in FROM, wrapped in brackets, and be queried like a table.

Give it a name

MySQL, SQL Server and PostgreSQL before version 16 require an alias, like AS t. Always add one.

Aggregate twice

Averages of totals, counts of groups, maximum of sums: any 'aggregate of an aggregate' needs this pattern.

Join to it

You can join a derived table to other tables, like customers to their per-customer totals.

See it in code

Walk through the code

sql
1SELECT2  COUNT(*)                                AS customers,3  ROUND(AVG(t.total_spent), 2)            AS avg_spend_per_customer,4  MAX(t.total_spent)                      AS top_spend,5  COUNT(*) FILTER (WHERE t.total_spent > 10000) AS above_10k6FROM (7  SELECT customer_id, SUM(amount) AS total_spent8  FROM orders9  WHERE status = 'delivered'10  GROUP BY customer_id11) AS t;

1/3The derived table t: one row per customer with their total.

Spend per customer, summarised. FILTER is PostgreSQL-only; in MySQL write SUM(t.total_spent > 10000), and in SQL Server SUM(CASE WHEN t.total_spent > 10000 THEN 1 ELSE 0 END).

Save yourself the pain

Common mistakes

Oops

Writing FROM (SELECT ...) without AS t.

Do this instead

Most databases reject it. Always name derived tables, ideally with a meaningful alias.

Oops

Using AVG(amount) on orders and calling it 'average per customer'.

Do this instead

That's average per order. Sum per customer first in a derived table, then average.

Oops

Adding ORDER BY inside the derived table to control the final order.

Do this instead

The outer query can reorder rows freely. Put ORDER BY on the outer query.

Your mission

Two-level summary

~20 min

Quick check

Why can't you write AVG(SUM(amount)) in a single GROUP BY query in PostgreSQL?

Go deeper

  1. 1Calculate revenue per customer in a subquery, then average it outside.
  2. 2Remember most databases require an alias for derived tables.
  3. 3Compare a nested derived table with the same logic written as a CTE.

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.

Download
InsideEdge