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
idnamecountrysignup_date
1SamGermany2026-01-02
2NinaCanada2026-01-05
3DavidGermany2026-01-10
4MayaJapan2026-01-15
5DanielCanada2026-01-20
… 5 more rows
orders
idcustomer_idorder_date
10112026-01-05
10222026-01-08
10332026-01-12
10412026-01-20
10542026-02-01
… 23 more rows
payments
idorder_idamount
6011016200
6021023500
6031033600
60410460000
60510512000
… 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.”
SQL editor Ctrl+Enter to run
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.

Case closed.

Ready for the next one?

Next: Case 018 — Above Average →