The Most Frequently Ordered Products for Each Customer - Problem

You're tasked with analyzing customer purchasing patterns in an e-commerce database. Given three tables - Customers, Orders, and Products - your job is to identify each customer's most frequently ordered products.

The Challenge: Some customers might have multiple products tied for "most frequently ordered" (same order count), and you need to return all of them. Only consider customers who have placed at least one order.

Tables Structure:

  • Customers: Contains customer information (customer_id, name)
  • Orders: Contains order details (order_id, order_date, customer_id, product_id)
  • Products: Contains product information (product_id, product_name, price)

Goal: Return a result set with customer_id, product_id, and product_name for each customer's most frequently ordered product(s).

Input & Output

example_1.sql โ€” Basic Case
$ Input: Customers: [(1,'Alice'), (2,'Bob'), (3,'Charlie')]\nOrders: [(1,'2023-01-01',1,101), (2,'2023-01-02',1,101), (3,'2023-01-03',1,102), (4,'2023-01-04',2,101), (5,'2023-01-05',2,103), (6,'2023-01-06',2,103)]\nProducts: [(101,'Laptop'), (102,'Mouse'), (103,'Keyboard')]
โ€บ Output: [(1,101,'Laptop'), (2,103,'Keyboard')]
๐Ÿ’ก Note: Customer 1 ordered Laptop twice and Mouse once, so Laptop is most frequent. Customer 2 ordered Laptop once and Keyboard twice, so Keyboard is most frequent.
example_2.sql โ€” Tie Case
$ Input: Customers: [(1,'Alice'), (2,'Bob')]\nOrders: [(1,'2023-01-01',1,101), (2,'2023-01-02',1,102), (3,'2023-01-03',2,101), (4,'2023-01-04',2,102), (5,'2023-01-05',2,103)]\nProducts: [(101,'Laptop'), (102,'Mouse'), (103,'Keyboard')]
โ€บ Output: [(1,101,'Laptop'), (1,102,'Mouse'), (2,101,'Laptop'), (2,102,'Mouse'), (2,103,'Keyboard')]
๐Ÿ’ก Note: Customer 1 has a tie between Laptop and Mouse (both ordered once). Customer 2 has a three-way tie (all products ordered once). All tied products are returned.
example_3.sql โ€” Single Product
$ Input: Customers: [(1,'Alice'), (2,'Bob')]\nOrders: [(1,'2023-01-01',1,101), (2,'2023-01-02',1,101), (3,'2023-01-03',1,101), (4,'2023-01-04',2,102)]\nProducts: [(101,'Laptop'), (102,'Mouse')]
โ€บ Output: [(1,101,'Laptop'), (2,102,'Mouse')]
๐Ÿ’ก Note: Customer 1 ordered only Laptop (3 times), Customer 2 ordered only Mouse (1 time). Each customer's most frequent product is their only product.

Visualization

Tap to expand
Customer 1Orders:๐Ÿ“ฑ Phone: 5 times๐Ÿ’ป Laptop: 2 times๐Ÿ–ฑ๏ธ Mouse: 1 timeWinner: ๐Ÿ“ฑ PhoneCustomer 2Orders:โŒจ๏ธ Keyboard: 3 times๐Ÿ–ฑ๏ธ Mouse: 3 times๐Ÿ“ฑ Phone: 1 timeWinners: โŒจ๏ธ ๐Ÿ–ฑ๏ธCustomer 3Orders:๐Ÿ’ป Laptop: 4 times๐Ÿ–จ๏ธ Printer: 2 times๐Ÿ“ฑ Phone: 1 timeWinner: ๐Ÿ’ป LaptopRANK() Window Function Process1. COUNT(*) GROUP BY customer_id, product_id2. PARTITION BY customer_id (separate groups)3. ORDER BY COUNT(*) DESC (rank by frequency)4. WHERE rank_num = 1 (get top products)โœจ Handles ties automatically!Final ResultsCustomer 1: ๐Ÿ“ฑ Phone (product_id: 101)Customer 2: โŒจ๏ธ Keyboard (product_id: 102)Customer 2: ๐Ÿ–ฑ๏ธ Mouse (product_id: 103)Customer 3: ๐Ÿ’ป Laptop (product_id: 104)
Understanding the Visualization
1
Count Orders
Count how many times each customer ordered each product
2
Partition by Customer
Group the data by customer to analyze each customer separately
3
Rank by Frequency
Within each customer group, rank products by order count (highest first)
4
Select Top Ranked
Filter to keep only products with rank = 1 (most frequent)
Key Takeaway
๐ŸŽฏ Key Insight: Window functions with PARTITION BY allow us to perform rankings within groups efficiently, making this the optimal solution for finding top items per category.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Single pass to count orders plus sorting for window function ranking

n
2n
โšก Linearithmic
Space Complexity
O(n)

Storage for grouped counts and ranking results

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค customer_id โ‰ค 104
  • 1 โ‰ค product_id โ‰ค 104
  • 1 โ‰ค order_id โ‰ค 105
  • No customer orders the same product more than once per day
  • All customer_id values in Orders exist in Customers table
  • All product_id values in Orders exist in Products table
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28 Netflix 15
52.8K Views
High Frequency
~18 min Avg. Time
1.8K 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