Beginner
Case 9 of 50
Case 009: Orders Per Customer
customersorders
The manager is putting together ByteMart's first loyalty program, and wants to know who actually qualifies before deciding on the tiers.
"How many times has each person actually ordered? I need the full list — everyone, even the customers who've never bought a thing."
The data Alex is looking at
customers
| id | name | country |
|---|---|---|
| 1 | Sam | Germany |
| 2 | Nina | Canada |
| 3 | David | Brazil |
| 4 | Maya | Japan |
| 5 | Daniel | Australia |
| … 1 more row | ||
orders
| id | customer_id | product | amount | order_date |
|---|---|---|---|---|
| 101 | 1 | Keyboard | 2500 | 2026-01-05 |
| 102 | 3 | Mouse | 1200 | 2026-01-08 |
| 103 | 2 | Headphones | 3500 | 2026-01-10 |
| 104 | 4 | Monitor | 12000 | 2026-01-12 |
| 105 | 1 | Webcam | 4500 | 2026-02-03 |
| … 4 more rows | ||||
TASK
Help Alex answer the manager:
“Show how many orders each customer has placed (including customers with zero).”
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.name,
COUNT(orders.id) AS order_count
FROM customers
LEFT JOIN orders ON customers.id = orders.customer_id
GROUP BY customers.name;
What that query returns
Case Debrief
GROUP BY, COUNT
- GROUP BY collapses rows into groups; COUNT() tells you how many rows landed in each group.
- COUNT(column) skips NULLs (empty/missing values) — exactly why it correctly shows 0 for unmatched LEFT JOIN rows, instead of 1.