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
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
โก Linearithmic
Space Complexity
O(n)
Storage for grouped counts and ranking results
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code