Beginner
Case 17 of 50
Case 017: The Loyal High-Value Customers
customersorderspayments
The VIP list from before turned out too big to actually act on — the outreach team can't call two hundred people. The manager comes back with a narrower ask.
"Could you find customers who are both loyal AND valuable this time — 3+ orders, and over $10,000 spent? One list, both conditions, so we're only calling people worth the time."
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:
“Find customers with at least 3 orders AND total spending over $10,000, showing their order count and total spent.”
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,
SUM(payments.amount) AS total_spent
FROM customers
JOIN orders ON customers.id = orders.customer_id
JOIN payments ON payments.order_id = orders.id
GROUP BY customers.name
HAVING COUNT(orders.id) >= 3 AND SUM(payments.amount) > 10000;
What that query returns
Case Debrief
HAVING with multiple conditions
- HAVING can check more than one condition at once, joined with AND — just like a normal WHERE. Here it's HAVING COUNT(orders.id) >= 3 AND SUM(payments.amount) > 10000, so a customer only makes the list if BOTH are true: 3 or more orders, and over $10,000 spent.
- A customer with 5 orders but only $8,000 spent doesn't qualify. Neither does someone who spent $50,000 in a single order. Both conditions have to pass together, not just one.