Layout

Buyers with more than three orders

PROBLEM 6 / 24 · BASICAll problems ↗

Find buyers with strictly more than three completed orders. Return customer_id and completed_order_count. Row order does not matter.

Outputcustomer_idcompleted_order_count

query.sql

DuckDB SQL

Restoring your saved draft…

⌘ / Ctrl + Enter to run

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_id
  • status completed / cancelled / pending

Fictional teaching data. Money is USD cents; timestamps are UTC.