LeetCode solutions

3865. Find Product Recommendation Pairs

My accepted SQL solution to LeetCode problem 3865, Find Product Recommendation Pairs, running in 570ms.

  • Difficulty: Medium
  • SQL
  • Runtime 570ms
  • Memory 0.0B

Read the problem on LeetCode View on GitHub

SQL

Accepted on LeetCode — runtime 570ms, memory 0.0B, accepted 2026-01-02.

sql
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
                            ORDER BY customer_count DESC, product1_id, product2_id

Source