Revenue inflated by a join (fan-out)
An SQL exercise from Querydrill. Solve it with a SQL editor in your browser, checked on the rows it returns and again on changed data, and it is checked the moment you submit.
Try it yourself
For each customer with at least one paid order (status paid), return the name, the total amount of their paid orders and the total quantity of items on those orders. An order without item lines counts with quantity 0. Customer names are unique.
customers (8 rows)
| id | name | country | signup_date |
|---|---|---|---|
| 1 | Anna | NL | 2024-01-05 |
| 2 | Bruno | BR | 2024-02-11 |
| 3 | Chloe | US | 2024-03-02 |
| 4 | Dev | IN | 2024-03-19 |
| 5 | Elif | TR | 2024-05-27 |
| 6 | Farah | US | 2024-06-14 |
| 7 | Gus | NL | 2024-08-01 |
| 8 | Hana | JP | 2024-09-09 |
orders (first 12 of 14 rows)
| id | customer_id | order_date | amount | status |
|---|---|---|---|---|
| 101 | 1 | 2025-01-03 | 120 | paid |
| 102 | 1 | 2025-01-20 | 80.5 | paid |
| 103 | 2 | 2025-01-22 | 200 | paid |
| 104 | 3 | 2025-02-02 | 45 | refunded |
| 105 | 3 | 2025-02-15 | 310 | paid |
| 106 | 4 | 2025-02-18 | 150 | pending |
| 107 | 5 | 2025-03-01 | 60 | paid |
| 108 | 6 | 2025-03-09 | 95 | paid |
| 109 | 6 | 2025-03-30 | 130 | paid |
| 110 | 6 | 2025-04-11 | 75 | paid |
| 111 | 7 | 2025-04-12 | 210 | paid |
| 112 | 1 | 2025-04-25 | 40 | paid |
order_items (first 12 of 18 rows)
| order_id | product_id | qty |
|---|---|---|
| 101 | 3 | 4 |
| 101 | 4 | 1 |
| 102 | 7 | 2 |
| 103 | 2 | 1 |
| 104 | 6 | 1 |
| 105 | 5 | 2 |
| 105 | 4 | 3 |
| 106 | 1 | 2 |
| 107 | 3 | 6 |
| 108 | 5 | 1 |
| 109 | 7 | 1 |
| 109 | 6 | 2 |
Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.
What this exercise teaches
Joining orders to order_items repeats each order once per item line, so SUM(amount) adds the same order several times. Aggregating order_items to one row per order before the join keeps every order exactly once.
A hint if you get stuck
An order with two item lines appears twice after the join. Bring order_items down to one row per order first.
Background you need
Common mistakes on this exercise
- An order with two item lines is joined twice, so its amount is added twice.
- SUM(DISTINCT) hides the repeats but also drops a second order with the same amount, and the inner join loses orders without item lines.
Each of these returns a result that looks plausible. The checker catches them because it runs your query on changed data as well.
Keep going on the practice page
Your first 8 exercises on Querydrill are free, with no account. The practice page has the full editor, timed mode and all the exercises in order. A free account shows 3 reference solutions a day; Pro unlocks every exercise, every solution, timed mode and the certificate.
Open this exerciseMore practice on the same topic
More exercises in Joins
- Orders with customer names
- Customers who never ordered
- Revenue per country
- Everyone with a department
- Employee and manager
- Revenue per product
- Revenue per category
- Products never sold
- Headcount per department
- Orders per customer
- Most recent order per customer
- Products per order
- Customers from the same country
- Customers who never had a refund
- Employees who manage nobody
- Categories per customer
- Payroll per department
- Direct reports per manager
- Stack two lists with UNION ALL
- Keep both sides with a full join
- Products on each order as one text (string_agg)
- Customers whose every order is paid (bool_and)
- Not equal, and the rows with NULL
- Overlapping bookings
- Compare two tables (EXCEPT)