Customer Order Frequency - Problem

You're managing an e-commerce platform and need to identify your most valuable customers - those who consistently spend big every month! ๐Ÿ’ฐ

Given three tables:

  • Customers: Contains customer information (ID, name, country)
  • Product: Contains product details (ID, description, price)
  • Orders: Contains order transactions (order ID, customer ID, product ID, date, quantity)

Your task is to find customers who spent at least $100 in BOTH June AND July 2020. These are your premium customers who deserve special attention!

Think of it like finding customers who hit a spending milestone in consecutive months - they're likely to be your most loyal buyers.

Return the customer_id and customer_name of these high-value customers in any order.

Input & Output

example_1.sql โ€” Basic Case
$ Input: Customers: [(1,'Winston','USA'),(2,'Jonathan','Peru'),(3,'Moustafa','Egypt')] Product: [(10,'LC Phone',300),(20,'LC T-Shirt',10),(30,'LC Book',45),(40,'LC Keychain',2)] Orders: [(1,1,10,'2020-06-10',1),(2,1,20,'2020-07-01',1),(3,1,30,'2020-07-08',2),(4,2,10,'2020-06-15',2),(5,2,40,'2020-07-01',10),(6,3,20,'2020-06-24',2),(7,3,30,'2020-06-25',2),(9,3,30,'2020-05-08',3)]
โ€บ Output: [(1,'Winston')]
๐Ÿ’ก Note: Winston spent $300 in June (1 LC Phone) and $110 in July (1 LC T-Shirt + 2 LC Books = $10 + $90). Both months >= $100, so he qualifies.
example_2.sql โ€” Multiple Qualified Customers
$ Input: Customers: [(1,'Alice','USA'),(2,'Bob','Canada'),(3,'Carol','UK')] Product: [(1,'Laptop',1000),(2,'Mouse',25),(3,'Keyboard',50)] Orders: [(1,1,1,'2020-06-05',1),(2,1,2,'2020-07-10',4),(3,2,1,'2020-06-20',1),(4,2,3,'2020-07-15',2),(5,3,2,'2020-06-30',5)]
โ€บ Output: [(1,'Alice'),(2,'Bob')]
๐Ÿ’ก Note: Alice: $1000 (June) + $100 (July) both >= $100. Bob: $1000 (June) + $100 (July) both >= $100. Carol only has June orders.
example_3.sql โ€” Edge Case - No Qualified Customers
$ Input: Customers: [(1,'David','France'),(2,'Eve','Spain')] Product: [(1,'Pen',5),(2,'Notebook',15)] Orders: [(1,1,1,'2020-06-01',10),(2,1,2,'2020-07-01',3),(3,2,1,'2020-06-15',5),(4,2,2,'2020-08-01',10)]
โ€บ Output: []
๐Ÿ’ก Note: David spent $50 (June) + $45 (July), both under $100. Eve has no July 2020 orders. No customers qualify.

Visualization

Tap to expand
OrdersJun-JulProductsPricesCustomersNamesCustomer AggregationJune: $120July: $150Customer ID: 1Filter: Both >= $100โœ“ VIPFinal VIP Customerscustomer_id, customer_name
Understanding the Visualization
1
Data Join
Combine Orders with Product prices and Customer info
2
Monthly Grouping
Group transactions by customer and calculate monthly totals
3
Threshold Filter
Keep only customers with $100+ spending in BOTH target months
Key Takeaway
๐ŸŽฏ Key Insight: Use conditional aggregation (CASE statements) to calculate spending for multiple months in a single query, then filter with HAVING clause for efficiency.

Time & Space Complexity

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

Single pass through Orders table with efficient grouping

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

Space for grouping results where k is number of unique customers

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค customers.length โ‰ค 100
  • 1 โ‰ค product.length โ‰ค 100
  • 1 โ‰ค orders.length โ‰ค 1000
  • Product prices are between $1 and $1000
  • Order quantities are between 1 and 100
  • All dates are in format 'YYYY-MM-DD'
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28
42.8K Views
High Frequency
~15 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