Find Product Recommendation Pairs - Problem

Amazon's "Customers who bought this item also bought" feature is one of the most powerful recommendation systems in e-commerce! ๐Ÿ›’

You're tasked with building the core algorithm that identifies product pairs that are frequently purchased together by the same customers. This classic market basket analysis problem helps Amazon suggest relevant products and increase sales.

Given:

  • ProductPurchases table: Contains user purchases with (user_id, product_id, quantity)
  • ProductInfo table: Contains product details with (product_id, category, price)

Goal: Find all product pairs where at least 3 different customers have purchased both products. For each qualifying pair, count how many customers bought both items.

Output Requirements:

  • Return pairs as (product1_id, product2_id) where product1_id < product2_id
  • Include the customer count for each pair
  • Sort by customer_count DESC, then product1_id ASC, then product2_id ASC

Input & Output

example_1.sql โ€” Basic Product Pairs
$ Input: ProductPurchases: | user_id | product_id | quantity | |---------|------------|-----------| | 1 | 101 | 2 | | 1 | 102 | 1 | | 2 | 101 | 1 | | 2 | 102 | 3 | | 3 | 101 | 1 | | 3 | 102 | 2 | | 4 | 103 | 1 | ProductInfo: | product_id | category | price | |------------|-------------|-------| | 101 | Electronics | 299.99| | 102 | Electronics | 149.99| | 103 | Books | 29.99 |
โ€บ Output: | product1_id | product2_id | customer_count | |-------------|-------------|----------------| | 101 | 102 | 3 |
๐Ÿ’ก Note: Products 101 and 102 were purchased together by users 1, 2, and 3 (exactly 3 customers), so this pair qualifies. Product 103 was only bought by user 4, so it doesn't form any qualifying pairs.
example_2.sql โ€” Multiple Qualifying Pairs
$ Input: ProductPurchases: | user_id | product_id | quantity | |---------|------------|-----------| | 1 | 101 | 1 | | 1 | 102 | 1 | | 1 | 103 | 2 | | 2 | 101 | 2 | | 2 | 102 | 1 | | 3 | 101 | 1 | | 3 | 102 | 3 | | 4 | 101 | 1 | | 4 | 103 | 1 | | 5 | 102 | 2 | | 5 | 103 | 1 |
โ€บ Output: | product1_id | product2_id | customer_count | |-------------|-------------|----------------| | 101 | 102 | 3 | | 101 | 103 | 2 | | 102 | 103 | 2 |
๐Ÿ’ก Note: Only pair (101,102) qualifies with 3 customers (users 1,2,3). Pairs (101,103) and (102,103) each have only 2 customers, falling short of the 3-customer minimum requirement.
example_3.sql โ€” Edge Case with Sorting
$ Input: ProductPurchases: | user_id | product_id | quantity | |---------|------------|-----------| | 1 | 201 | 1 | | 1 | 202 | 1 | | 1 | 301 | 1 | | 2 | 201 | 2 | | 2 | 202 | 1 | | 2 | 301 | 2 | | 3 | 201 | 1 | | 3 | 202 | 1 | | 3 | 301 | 1 | | 4 | 201 | 1 | | 4 | 301 | 1 | | 5 | 201 | 2 | | 5 | 301 | 1 |
โ€บ Output: | product1_id | product2_id | customer_count | |-------------|-------------|----------------| | 201 | 301 | 5 | | 201 | 202 | 3 |
๐Ÿ’ก Note: Both pairs qualify, but (201,301) has higher customer count (5) so appears first. When counts are equal, we'd sort by product1_id ASC, then product2_id ASC. Here (201,301) comes before (201,202) due to higher customer count.

Visualization

Tap to expand
Amazon Product Recommendation AnalysisCustomer 1 Cart๐Ÿ“ฑPhone๐ŸŽงHeadphones๐Ÿ’ปLaptopCustomer 2 Cart๐Ÿ“ฑPhone๐ŸŽงHeadphonesCustomer 3 Cart๐Ÿ“ฑPhone๐ŸŽงHeadphonesโŒšWatchCommon Pattern: Phone + HeadphonesSelf-Join Analysis ResultProduct PairCustomer CountRecommendation?Phone + Headphones3 customersโœ“ YES๐ŸŽฏ Key Insight: Self-Join Magic!By joining ProductPurchases with itself on user_id, we efficiently generate all product pairsper customer, then GROUP BY to count co-purchases - perfect for recommendation systems!
Understanding the Visualization
1
Collect Shopping Data
Gather all customer purchase records with user IDs and product IDs
2
Generate Product Pairs
For each customer, create all possible product combinations from their purchases
3
Count Co-purchases
Group identical product pairs and count how many different customers bought both
4
Filter & Rank
Keep pairs with 3+ customers and rank by popularity for recommendations
Key Takeaway
๐ŸŽฏ Key Insight: Self-join approach efficiently generates product pairs per customer and leverages SQL's GROUP BY to count co-purchases, making it perfect for real-world recommendation systems like Amazon's 'customers who bought this also bought' feature.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(Nร—P_avgยฒ)

N customers, P_avgยฒ for product pairs per customer on average

n
2n
โœ“ Linear Growth
Space Complexity
O(Pยฒ)

Storage for unique product pairs in result

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค number of users โ‰ค 104
  • 1 โ‰ค number of products โ‰ค 103
  • 1 โ‰ค quantity โ‰ค 100
  • At least 3 different customers must purchase both products for a pair to qualify
  • Product pairs must satisfy product1_id < product2_id condition
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28 Netflix 25
34.2K Views
High Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen