TheAnalyst / SQLAll lessons ↗
Day 14 · Aggregation & joins · 10–20 minutes

Join and aggregate

Join related tables before grouping when a summary needs attributes from both.

01 · Learn

The idea

Join related tables before grouping when a summary needs attributes from both. Read the query line by line and identify the input table, the transformation, and the shape of the result before running it.

SELECT c.name, SUM(o.amount) AS revenue FROM customers c JOIN orders o ON o.customer_id = c.customer_id GROUP BY c.customer_id, c.name ORDER BY revenue DESC;
Read it actively

Before running the example, predict the output columns and whether the query returns one row, many rows, or possibly no rows.

02 · Recall

Explain it without looking

In your own words, explain the main SQL feature in today’s lesson and why it answers the question.

03 · Solve

Run your own query

Show each customer's name and total revenue, highest first.

Loading browser SQL engine…
04 · Repeat

Close the loop