diff --git a/README.md b/README.md index 6aec9a2..0bb5d95 100644 --- a/README.md +++ b/README.md @@ -187,6 +187,7 @@ Useful for preparing for technical interviews and improving your SQL skills. - [3421. Find Students Who Improved](./leetcode/medium/3421.%20Find%20Students%20Who%20Improved.sql) - [3475. DNA Pattern Recognition](./leetcode/medium/3475.%20DNA%20Pattern%20Recognition.sql) - [3497. Analyze Subscription Conversion](./leetcode/medium/3497.%20Analyze%20Subscription%20Conversion.sql) + - [3521. Find Product Recommendation Pairs](./leetcode/medium/3521.%20Find%20Product%20Recommendation%20Pairs.sql) - [3564. Seasonal Sales Analysis](./leetcode/medium/3564.%20Seasonal%20Sales%20Analysis.sql) - [3601. Find Drivers with Improved Fuel Efficiency](./leetcode/medium/3601.%20Find%20Drivers%20with%20Improved%20Fuel%20Efficiency.sql) 3. [Hard](./leetcode/hard/) diff --git a/leetcode/medium/3521. Find Product Recommendation Pairs.sql b/leetcode/medium/3521. Find Product Recommendation Pairs.sql new file mode 100644 index 0000000..bda4396 --- /dev/null +++ b/leetcode/medium/3521. Find Product Recommendation Pairs.sql @@ -0,0 +1,53 @@ +/* +Question 3521. Find Product Recommendation Pairs +Link: https://leetcode.com/problems/find-product-recommendation-pairs/description/?envType=problem-list-v2&envId=database + +Table: ProductPurchases + ++-------------+------+ +| Column Name | Type | ++-------------+------+ +| user_id | int | +| product_id | int | +| quantity | int | ++-------------+------+ +(user_id, product_id) is the unique key for this table. +Each row represents a purchase of a product by a user in a specific quantity. +Table: ProductInfo + ++-------------+---------+ +| Column Name | Type | ++-------------+---------+ +| product_id | int | +| category | varchar | +| price | decimal | ++-------------+---------+ +product_id is the primary key for this table. +Each row assigns a category and price to a product. +Amazon wants to implement the Customers who bought this also bought... feature based on co-purchase patterns. Write a solution to : + +Identify distinct product pairs frequently purchased together by the same customers (where product1_id < product2_id) +For each product pair, determine how many customers purchased both products +A product pair is considered for recommendation if at least 3 different customers have purchased both products. + +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. +*/ + +SELECT + p1.product_id AS product1_id, + p2.product_id AS product2_id, + p1.category AS product1_category, + p2.category AS product2_category, + COUNT(DISTINCT pp1.user_id) AS customer_count +FROM ProductInfo AS p1 +CROSS JOIN ProductInfo AS p2 +RIGHT JOIN --noqa: CV08 + ProductPurchases AS pp1 + ON p1.product_id = pp1.product_id +RIGHT JOIN --noqa: CV08 + ProductPurchases AS pp2 + ON p2.product_id = pp2.product_id AND pp1.user_id = pp2.user_id +WHERE p1.product_id < p2.product_id +GROUP BY p1.product_id, p2.product_id, p1.category, p2.category +HAVING COUNT(DISTINCT pp1.user_id) >= 3 AND COUNT(DISTINCT pp2.user_id) >= 3 +ORDER BY customer_count DESC, p1.product_id ASC, p2.product_id ASC