Orders per customer

aggregations

Orders per customer

Target SQL Interview Question

Target's customer success team wants to see how active each online customer is.

For each customer_id, count the orders they have placed as order_count. Count every order, whatever its status.

Asked of

  • Data Analyst
  • Business Analyst
  • BI Analyst
  • Product Analyst
  • Data Scientist
  • ML Engineer

ordersTable35 rows

Column NameType
order_idBIGINT
customer_idBIGINT
order_dateDATE
statusVARCHAR
amountDOUBLE

ordersExample Input

order_idcustomer_idorder_datestatusamount
100112024-01-05completed120.5
100222024-01-07completed2400
100312024-01-11completed89.99
100432024-01-15canceled45
100542024-01-18completed610.75
100622024-01-22completed1850.25
100752024-01-29completed3200
100812024-02-02refunded75
100962024-02-06completed55.4
101032024-02-09completed132.6

Example Output

customer_idorder_count
13
22
32
41
51
61

Explanation

Customer 1 placed orders 1001, 1003 and 1008, so their count is 3. Order 1004 was canceled, but it still counts toward customer 3's total of 2.

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