Who Shops Across Multiple Categories?
Whetcode Supply sells three quite separate things — electronics, furniture and office supplies — and the cross-sell team has a theory. A shopper who has already bought from two parts of the catalogue thinks of Whetcode as a general supplier, and will open a bundle offer. A shopper who has only ever bought one kind of thing will read the same email as spam.
So the team needs the first sort of customer picked out. Nothing in the order log says what kind of thing an order was: an order line records a product_id, and the fact that product 2 is furniture is recorded somewhere else entirely. And a customer with three orders that are all furniture must not qualify — it is the variety that matters, not the number of orders.
orders — one row per order line. product_id points at a row in products.
| id | customer_id | product_id | quantity | order_date |
|---|---|---|---|---|
| 1 | 1 | 1 | 2 | 2023-02-01 |
| 2 | 1 | 4 | 1 | 2023-02-01 |
| 3 | 2 | 2 | 1 | 2023-02-10 |
| 4 | 3 | 3 | 3 | 2023-03-05 |
| 5 | 4 | 5 | 5 | 2023-03-11 |
| 6 | 1 | 3 | 1 | 2023-04-02 |
| 7 | 5 | 1 | 4 | 2023-05-20 |
| 8 | 2 | 5 | 2 | 2023-05-22 |
| 9 | 3 | 4 | 1 | 2023-06-01 |
| 10 | 5 | 2 | 1 | 2023-06-15 |
products — one row per item on sale. category is the part of the catalogue the item belongs to.
| id | name | category | price |
|---|---|---|---|
| 1 | Wireless Mouse | electronics | 25.0 |
| 2 | Standing Desk | furniture | 350.0 |
| 3 | Desk Lamp | furniture | 40.0 |
| 4 | Mechanical Keyboard | electronics | 85.0 |
| 5 | Notebook Set | office | 12.0 |
Both tables already exist in the database — there is nothing to create or load.
Task: Write a query returning a single column, customer_id, holding every customer who has bought items from at least two different category values. Somebody with four orders all from the same category does not qualify. Hand the ids back smallest first.
Example output — shape only, on invented ids.
| customer_id |
|---|
| 4 |
| 7 |
| 9 |
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in