[SQL 문제 풀이] Find Product Recommendation Pairs (제품 추천 쌍 찾기)

Stupefyee's avatar
Jun 17, 2025
[SQL 문제 풀이] Find Product Recommendation Pairs (제품 추천 쌍 찾기)
Find Product Recommendation Pairs - LeetCode
Can you solve this real interview question? Find Product Recommendation Pairs - 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 : 1. Identify distinct product pairs frequently purchased together by the same customers (where product1_id < product2_id) 2. 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. The result format is in the following example.   Example: Input: ProductPurchases table: +---------+------------+----------+ | user_id | product_id | quantity | +---------+------------+----------+ | 1 | 101 | 2 | | 1 | 102 | 1 | | 1 | 103 | 3 | | 2 | 101 | 1 | | 2 | 102 | 5 | | 2 | 104 | 1 | | 3 | 101 | 2 | | 3 | 103 | 1 | | 3 | 105 | 4 | | 4 | 101 | 1 | | 4 | 102 | 1 | | 4 | 103 | 2 | | 4 | 104 | 3 | | 5 | 102 | 2 | | 5 | 104 | 1 | +---------+------------+----------+ ProductInfo table: +------------+-------------+-------+ | product_id | category | price | +------------+-------------+-------+ | 101 | Electronics | 100 | | 102 | Books | 20 | | 103 | Clothing | 35 | | 104 | Kitchen | 50 | | 105 | Sports | 75 | +------------+-------------+-------+ Output: +-------------+-------------+-------------------+-------------------+----------------+ | product1_id | product2_id | product1_category | product2_category | customer_count | +-------------+-------------+-------------------+-------------------+----------------+ | 101 | 102 | Electronics | Books | 3 | | 101 | 103 | Electronics | Clothing | 3 | | 102 | 104 | Books | Kitchen | 3 | +-------------+-------------+-------------------+-------------------+----------------+ Explanation: * Product pair (101, 102): * Purchased by users 1, 2, and 4 (3 customers) * Product 101 is in Electronics category * Product 102 is in Books category * Product pair (101, 103): * Purchased by users 1, 3, and 4 (3 customers) * Product 101 is in Electronics category * Product 103 is in Clothing category * Product pair (102, 104): * Purchased by users 2, 4, and 5 (3 customers) * Product 102 is in Books category * Product 104 is in Kitchen category The result is ordered by customer_count in descending order. For pairs with the same customer_count, they are ordered by product1_id and then product2_id in ascending order.
Find Product Recommendation Pairs - LeetCode
notion image
아마존은 이것을 구매한 고객들도 공동 구매 패턴을 기반으로 구매한 기능을 구현하고자 합니다. 해결책을 작성하세요: 1. 동일한 고객이 자주 함께 구매하는 제품 쌍을 식별합니다(여기서 product1_id < product2_id) 2. 각 제품 쌍에 대해 두 제품을 모두 구매한 고객 수를 결정합니다 * 두 제품을 모두 구매한 고객이 최소 3명 이상인 경우 제품 쌍을 추천 대상으로 고려합니다. customer_count에 의해 내림차순으로 정렬된 결과 테이블을 반환하고, 동점일 경우에는 product1_id에 의해 오름차순으로, product2_id에 의해 오름차순으로 반환합니다.
 

내가 작성한 쿼리

MySQL, Oracle

SELECT pp1.product_id AS product1_id, pp2.product_id AS product2_id, pi1.category AS product1_category, pi2.category AS product2_category, COUNT(DISTINCT pp1.user_id) AS customer_count FROM ProductPurchases pp1 JOIN ProductPurchases pp2 ON pp1.user_id = pp2.user_id AND pp1.product_id < pp2.product_id -- 중복 방지 JOIN ProductInfo pi1 ON pp1.product_id = pi1.product_id JOIN ProductInfo pi2 ON pp2.product_id = pi2.product_id GROUP BY pp1.product_id, pp2.product_id, pi1.category, pi2.category HAVING COUNT(DISTINCT pp1.user_id) >= 3 -- 3명이상 구매한 경우 ORDER BY customer_count DESC, product1_id ASC, product2_id ASC;
Share article

stupefyee