Skip to content

Commit 5602b16

Browse files
committed
task: #3521
1 parent abe2ed4 commit 5602b16

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ Useful for preparing for technical interviews and improving your SQL skills.
187187
- [3421. Find Students Who Improved](./leetcode/medium/3421.%20Find%20Students%20Who%20Improved.sql)
188188
- [3475. DNA Pattern Recognition](./leetcode/medium/3475.%20DNA%20Pattern%20Recognition.sql)
189189
- [3497. Analyze Subscription Conversion](./leetcode/medium/3497.%20Analyze%20Subscription%20Conversion.sql)
190+
- [3521. Find Product Recommendation Pairs](./leetcode/medium/3521.%20Find%20Product%20Recommendation%20Pairs.sql)
190191
- [3564. Seasonal Sales Analysis](./leetcode/medium/3564.%20Seasonal%20Sales%20Analysis.sql)
191192
- [3601. Find Drivers with Improved Fuel Efficiency](./leetcode/medium/3601.%20Find%20Drivers%20with%20Improved%20Fuel%20Efficiency.sql)
192193
3. [Hard](./leetcode/hard/)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Question 3521. Find Product Recommendation Pairs
3+
Link: https://leetcode.com/problems/find-product-recommendation-pairs/description/?envType=problem-list-v2&envId=database
4+
5+
Table: ProductPurchases
6+
7+
+-------------+------+
8+
| Column Name | Type |
9+
+-------------+------+
10+
| user_id | int |
11+
| product_id | int |
12+
| quantity | int |
13+
+-------------+------+
14+
(user_id, product_id) is the unique key for this table.
15+
Each row represents a purchase of a product by a user in a specific quantity.
16+
Table: ProductInfo
17+
18+
+-------------+---------+
19+
| Column Name | Type |
20+
+-------------+---------+
21+
| product_id | int |
22+
| category | varchar |
23+
| price | decimal |
24+
+-------------+---------+
25+
product_id is the primary key for this table.
26+
Each row assigns a category and price to a product.
27+
Amazon wants to implement the Customers who bought this also bought... feature based on co-purchase patterns. Write a solution to :
28+
29+
Identify distinct product pairs frequently purchased together by the same customers (where product1_id < product2_id)
30+
For each product pair, determine how many customers purchased both products
31+
A product pair is considered for recommendation if at least 3 different customers have purchased both products.
32+
33+
Return the result table ordered by customer_count in descending order, and in case of a tie, by product1_id in ascending order, and then by product2_id in ascending order.
34+
*/
35+
36+
SELECT
37+
p1.product_id AS product1_id,
38+
p2.product_id AS product2_id,
39+
p1.category AS product1_category,
40+
p2.category AS product2_category,
41+
COUNT(DISTINCT pp1.user_id) AS customer_count
42+
FROM ProductInfo AS p1
43+
CROSS JOIN ProductInfo AS p2
44+
RIGHT JOIN --noqa: CV08
45+
ProductPurchases AS pp1
46+
ON p1.product_id = pp1.product_id
47+
RIGHT JOIN --noqa: CV08
48+
ProductPurchases AS pp2
49+
ON p2.product_id = pp2.product_id AND pp1.user_id = pp2.user_id
50+
WHERE p1.product_id < p2.product_id
51+
GROUP BY p1.product_id, p2.product_id, p1.category, p2.category
52+
HAVING COUNT(DISTINCT pp1.user_id) >= 3 AND COUNT(DISTINCT pp2.user_id) >= 3
53+
ORDER BY customer_count DESC, p1.product_id ASC, p2.product_id ASC

0 commit comments

Comments
 (0)