← Interview questions
Amazon logo

Category Explorers

Amazon·medium·SQL

Find all customers who, across completed orders in 2023, purchased items from at least 3 distinct product categories. Return customer_id, name, and the number of distinct categories they purchased from, ordered by category count descending.

Schema

customers
customer_idINTEGER
nameVARCHAR
emailVARCHAR
orders
order_idINTEGER
customer_idINTEGER
order_dateDATE
statusVARCHAR
status values: 'completed', 'cancelled'
order_items
item_idINTEGER
order_idINTEGER
product_idINTEGER
quantityINTEGER
priceDECIMAL
products
product_idINTEGER
nameVARCHAR
categoryVARCHAR

Target output

Target output
customer_idnamecategories_purchased
1Sarah Okonkwo4
2James Park3
3Maria Santos3
Querycustomers, orders, order_items, products
Cmd+Enterto runLoading...
Output

Loading sample data...