Orders per customer segment

joins

Orders per customer segment

Amazon SQL Interview Question

Amazon sells to individual shoppers and, through Amazon Business, to small businesses and large enterprises. The sales team splits customers into these three segments and wants to know which segment places the most orders.

For each segment, count the orders placed by its customers as order_count. Count every order, whatever its status.

Asked of

  • Data Analyst
  • Business Analyst
  • BI Analyst
  • Product Analyst

ordersTable35 rows

Column NameType
order_idBIGINT
customer_idBIGINT
order_dateDATE
statusVARCHAR
amountDOUBLE

customersTable10 rows

Column NameType
customer_idBIGINT
nameVARCHAR
countryVARCHAR
segmentVARCHAR
signup_dateDATE

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

customersExample Input

customer_idnamecountrysegmentsignup_date
1Priya SharmaIndiaconsumer2023-01-14
2Marcus WebbUnited Statesenterprise2023-02-03
3Ana SousaBrazilconsumer2023-02-27
4Liam O'ConnorIrelandsmall_business2023-03-15
5Yuki TanakaJapanenterprise2023-04-02
6Fatima Al-RashidUnited Arab Emiratesconsumer2023-05-21
7Daniel OkaforNigeriasmall_business2023-06-08

Example Output

segmentorder_count
consumer6
enterprise3
small_business1

Explanation

Three consumer customers placed orders in the example: Priya Sharma, Ana Sousa and Fatima Al-Rashid. Together they placed 6 orders, so the consumer segment's count is 6, not 3.

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