Customers with repeat orders

aggregations

Customers with repeat orders

Target SQL Interview Question

Target's loyalty team wants to reward repeat buyers, which it defines as customers who have placed more than four online orders.

Find every repeat buyer, showing their customer_id and their number of orders as order_count. Count every order, whatever its status.

Asked of

  • Data Analyst
  • Business Analyst
  • Product Analyst
  • Analytics Engineer

ordersTable35 rows

Column NameType
order_idBIGINT
customer_idBIGINT
order_dateDATE
statusVARCHAR
amountDOUBLE

ordersExample Input

order_idcustomer_idorder_datestatusamount
100112024-01-05completed120.5
100312024-01-11completed89.99
100432024-01-15canceled45
100812024-02-02refunded75
101032024-02-09completed132.6
101512024-03-01completed210
101832024-03-12completed178.25
102412024-04-05completed95.25
102632024-04-13refunded88
103212024-05-07completed143

Example Output

customer_idorder_count
16

Explanation

Customer 1 placed six orders in the example, including one that was refunded, so they qualify with 6. Customer 3 placed four orders, which is not more than four, so they are left out.

The example above is a small slice of the data. Your query runs against the full tables.