Amazon wants to understand shopping patterns across product categories. You have two tables:
ProductPurchases table contains user purchase data with columns user_id, product_id, and quantity.
ProductInfo table contains product details with columns product_id, category, and price.
Write a solution to:
- Find all category pairs where
category1 < category2alphabetically - For each category pair, count the number of unique customers who purchased products from both categories
- Only include category pairs where at least 3 different customers have purchased from both categories
Return results ordered by customer_count descending, then by category1 ascending, then by category2 ascending.
Table Schema
| Column Name | Type | Description |
|---|---|---|
user_id
PK
|
int | User identifier |
product_id
PK
|
int | Product identifier |
quantity
|
int | Quantity purchased |
| Column Name | Type | Description |
|---|---|---|
product_id
PK
|
int | Product identifier |
category
|
varchar | Product category |
price
|
decimal | Product price |
Input & Output
| user_id | product_id | quantity |
|---|---|---|
| 1 | 101 | 2 |
| 1 | 201 | 1 |
| 2 | 101 | 1 |
| 2 | 301 | 3 |
| 3 | 201 | 1 |
| 3 | 301 | 2 |
| 4 | 101 | 1 |
| 4 | 201 | 1 |
| product_id | category | price |
|---|---|---|
| 101 | Electronics | 299.99 |
| 201 | Books | 19.99 |
| 301 | Clothing | 49.99 |
| category1 | category2 | customer_count |
|---|
Users 1, 2, 3, and 4 make purchases across different categories. User 1 bought Electronics and Books, User 2 bought Electronics and Clothing, User 3 bought Books and Clothing, User 4 bought Electronics and Books. However, no category pair has at least 3 customers, so no pairs are reportable.
| user_id | product_id | quantity |
|---|---|---|
| 1 | 101 | 1 |
| 1 | 201 | 1 |
| 2 | 101 | 2 |
| 2 | 201 | 1 |
| 3 | 101 | 1 |
| 3 | 201 | 3 |
| 4 | 101 | 1 |
| 4 | 201 | 1 |
| product_id | category | price |
|---|---|---|
| 101 | Electronics | 299.99 |
| 201 | Books | 19.99 |
| category1 | category2 | customer_count |
|---|---|---|
| Books | Electronics | 4 |
All 4 users (1, 2, 3, 4) purchased products from both Books and Electronics categories. Since 4 ≥ 3, this category pair is reportable. The pair is ordered as 'Books', 'Electronics' since 'Books' < 'Electronics' alphabetically.
Constraints
-
1 ≤ user_id ≤ 10000 -
1 ≤ product_id ≤ 10000 -
1 ≤ quantity ≤ 100 -
categoryis a non-empty string -
priceis a positive decimal value