Advanced
Case 50 of 50
Case 050: The Shape of the Business
customersorderspayments
Alex's final report for the quarter — the one the manager will actually present.
"Could you give me one row per country? I want the whole picture — customers, orders, revenue, revenue per customer — and rank them while you're at it."
The data Alex is looking at
customers
| id | name | country | signup_date |
|---|---|---|---|
| 1 | Sam | Germany | 2026-01-02 |
| 2 | Nina | Canada | 2026-01-05 |
| 3 | David | Germany | 2026-01-10 |
| 4 | Maya | Japan | 2026-01-15 |
| 5 | Daniel | Canada | 2026-01-20 |
| … 5 more rows | |||
orders
| id | customer_id | order_date |
|---|---|---|
| 101 | 1 | 2026-01-05 |
| 102 | 2 | 2026-01-08 |
| 103 | 3 | 2026-01-12 |
| 104 | 1 | 2026-01-20 |
| 105 | 4 | 2026-02-01 |
| … 23 more rows | ||
payments
| id | order_id | amount |
|---|---|---|
| 601 | 101 | 6200 |
| 602 | 102 | 3500 |
| 603 | 103 | 3600 |
| 604 | 104 | 60000 |
| 605 | 105 | 12000 |
| … 23 more rows | ||
TASK
Help Alex answer the manager:
“Build one final report: for each country, show total customers, total orders, total revenue, revenue per customer, and each country's rank by revenue.”
Write a query above and hit Run to see what comes back.
No hints requested yet — click "Get a hint" when you're stuck.
Alex's final query — this is for reference. Copy it into your own thinking, not into the editor above.
SELECT
customers.country,
COUNT(DISTINCT customers.id) AS customers,
COUNT(DISTINCT orders.id) AS orders,
SUM(payments.amount) AS revenue,
ROUND(SUM(payments.amount) * 1.0 / COUNT(DISTINCT customers.id), 1) AS revenue_per_customer,
RANK() OVER (ORDER BY SUM(payments.amount) DESC) AS revenue_rank
FROM customers
JOIN orders ON orders.customer_id = customers.id
JOIN payments ON payments.order_id = orders.id
GROUP BY customers.country
ORDER BY revenue_rank;
What that query returns
Case Debrief
Capstone: full multi-metric dashboard
- Fifty cases ago, this looked impossible.
- Now it's JOIN, GROUP BY, COUNT DISTINCT, SUM, and a window function — every tool from this curriculum, aimed at one real business question.
- That's the whole trick. And you're not a beginner anymore.