Buyers with more than three orders
Find buyers with strictly more than three completed orders. Return customer_id and completed_order_count. Row order does not matter.
Output
customer_idcompleted_order_countquery.sql
DuckDB SQLRestoring your saved draft…
Run a query to see results.
Reveal solution & explanation
SELECT customer_id, COUNT(*) AS completed_order_count FROM orders WHERE status = 'completed' GROUP BY customer_id HAVING COUNT(*) > 3;
A group-level condition belongs in HAVING. Counting all statuses first would inflate repeat-purchase activity.
Data & schema
- order_id
- INTEGER · PK
- customer_id
- INTEGER
- ordered_at
- TIMESTAMP
- status
- VARCHAR
- order_total_cents
- INTEGER
Table notes & relationships
One row per order. Only completed orders count as revenue unless a question says otherwise. Times are UTC.
customer_id→ customers.customer_idstatuscompleted / cancelled / pending
Fictional teaching data. Money is USD cents; timestamps are UTC.