Top spending customers

joins

Top spending customers

Amazon SQL Interview Question

Amazon's loyalty team is inviting its three biggest customers to an exclusive preview event.

Find the three customers who have spent the most on completed orders. Show their name and total spend as total_spent. Sort the rows by total_spent, highest first.

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

nametotal_spent
Marcus Webb4250.25
Yuki Tanaka3200
Liam O'Connor610.75

Explanation

Marcus Webb spent 4,250.25 across two completed orders, more than anyone else in the example. Yuki Tanaka is second with 3,200 and Liam O'Connor is third with 610.75. Everyone else spent less, so they are not invited.

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