Capstone: total spent per customer
This is the query you'd actually write at a job: "who are our biggest
customers?" It needs everything from this module - a JOIN to connect orders
to the people who placed them, GROUP BY to bucket by customer, SUM to add
up what each one spent, and ORDER BY to rank them.
None of these pieces is new. What's new is stacking them: join first, group the
joined rows, aggregate within each group, then sort the final result. That
order - join, group, aggregate, sort - is the shape of most real reporting
queries, not just this one.
Same users and orders tables as the JOIN lesson.
Your task: for each user who has placed at least one order, return their
name and the total amount they've spent (as total_spent), ordered from
highest spender to lowest.
You'll practice:
Joining, grouping, and aggregating in a single query
Ordering by an aggregated column
Show a hint
Show solution
Related reading: INNER vs LEFT (and the Others) →
Previous Next