The Most Recent Orders for Each Product - Problem

๐Ÿ›’ The Most Recent Orders for Each Product

Imagine you're a data analyst for an e-commerce platform, and your manager asks you to generate a report showing the most recent order for each product in your inventory. This information is crucial for understanding recent customer purchasing patterns and product popularity trends.

You have access to three database tables:

  • Customers - Contains customer information with unique customer_id
  • Orders - Contains order details including order_date, customer_id, and product_id
  • Products - Contains product information with product_name and price

Your task is to write a SQL query that finds the most recent order(s) for each product. If multiple orders for the same product were placed on the same most recent date, include all of them.

The results should be ordered by:

  1. product_name (ascending)
  2. If tied, then by product_id (ascending)
  3. If still tied, then by order_id (ascending)

Input & Output

example_1.sql โ€” Basic Case
$ Input: Customers: [(1, 'Alice'), (2, 'Bob'), (3, 'Charlie')] Orders: [(101, '2024-03-10', 1, 1), (102, '2024-03-15', 2, 1), (103, '2024-03-12', 3, 2)] Products: [(1, 'Laptop', 999), (2, 'Mouse', 25)]
โ€บ Output: [('Laptop', '2024-03-15', 'Bob'), ('Mouse', '2024-03-12', 'Charlie')]
๐Ÿ’ก Note: For Laptop (product_id=1), the most recent order is from 2024-03-15 by Bob. For Mouse (product_id=2), the most recent order is from 2024-03-12 by Charlie.
example_2.sql โ€” Tie Case
$ Input: Customers: [(1, 'Alice'), (2, 'Bob')] Orders: [(101, '2024-03-15', 1, 1), (102, '2024-03-15', 2, 1)] Products: [(1, 'Laptop', 999)]
โ€บ Output: [('Laptop', '2024-03-15', 'Alice'), ('Laptop', '2024-03-15', 'Bob')]
๐Ÿ’ก Note: Both orders for Laptop were placed on the same most recent date (2024-03-15), so both are included in the result, ordered by order_id.
example_3.sql โ€” Multiple Products
$ Input: Customers: [(1, 'Alice'), (2, 'Bob')] Orders: [(101, '2024-03-10', 1, 1), (102, '2024-03-15', 2, 2), (103, '2024-03-20', 1, 3)] Products: [(1, 'Laptop', 999), (2, 'Mouse', 25), (3, 'Keyboard', 75)]
โ€บ Output: [('Keyboard', '2024-03-20', 'Alice'), ('Laptop', '2024-03-10', 'Alice'), ('Mouse', '2024-03-15', 'Bob')]
๐Ÿ’ก Note: Each product has one most recent order: Keyboard on 2024-03-20, Laptop on 2024-03-10, and Mouse on 2024-03-15. Results are ordered by product_name alphabetically.

Constraints

  • 1 โ‰ค Number of customers โ‰ค 104
  • 1 โ‰ค Number of orders โ‰ค 105
  • 1 โ‰ค Number of products โ‰ค 103
  • Each customer orders each product at most once per day
  • All dates are valid and in 'YYYY-MM-DD' format
  • Product names are unique and non-empty
  • Customer names are non-empty varchar fields

Visualization

Tap to expand
Finding Most Recent Orders - Visual WorkflowRaw OrdersGroup byProductSort byDate DESCRANK()FunctionFinalResultsWindow Function Magic: RANK() OVER (PARTITION BY product_id ORDER BY order_date DESC)Product: Laptop2024-03-20 | Rank: 1 โญ2024-03-15 | Rank: 22024-03-10 | Rank: 3Product: Mouse2024-03-18 | Rank: 1 โญ2024-03-18 | Rank: 1 โญ2024-03-12 | Rank: 3Product: Keyboard2024-03-22 | Rank: 1 โญ2024-03-16 | Rank: 22024-03-08 | Rank: 3โœจ Filter WHERE rank = 1 to get only the starred (โญ) rows!Notice how Mouse has 2 orders with Rank 1 (tied for most recent date)
Understanding the Visualization
1
Collect All Receipts
Gather all order records from the database
2
Group by Product
Organize receipts by product type (like sorting into product bins)
3
Sort by Date
Within each product group, arrange by date with newest first
4
Pick Top Receipt
Take the most recent receipt(s) from each product pile
5
Generate Report
Create final report ordered by product name for easy reading
Key Takeaway
๐ŸŽฏ Key Insight: Window functions like RANK() allow us to efficiently identify the "best" records in each group without expensive correlated subqueries. This single-pass approach scales beautifully with large datasets and handles ties gracefully.
Asked in
Amazon 75 Google 60 Meta 45 Microsoft 40 Apple 35
85.2K Views
High Frequency
~25 min Avg. Time
2.3K 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