Rank the Top Three Best-Selling ASINs in Each Category Monthly
Last updated: November 29, 2026
Quick Overview
Given order line items and a product catalog, return the top three ASINs by units sold within each product category for every calendar month, keeping ties.
Amazon
November 29, 20260
0
0 solved
Given order line items and a product catalog, return the top three ASINs by units sold within each product category for every calendar month, keeping ties.
A warm-up SQL item in the style of Amazon's data science and BIE online assessments, framed on retail catalog data. The interviewer checks that you aggregate before ranking, choose the right ranking function for ties, and exclude cancelled lines.
What the Interviewer Expects
- Aggregate units per category, month and ASIN before applying any window function
- Use DENSE_RANK or RANK deliberately and explain how ties are handled
- Partition the window by both category and month
- Filter out cancelled or returned lines and state the assumption
- Return a deterministic order in the final result
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would the answer change if you ranked by revenue instead of units?
- What if the business wants exactly three rows per category even with ties?
- How would you also show each ASIN's rank in the previous month?
- How would you handle an ASIN that moves between categories mid-month?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
Assume two tables:
order_items(order_id, asin, quantity, order_ts, line_status)with one row per line item.products(asin, category)with one current category per ASIN.
Questions I would ask:...
Approach
- Join line items to products to get the category.
- Aggregate
SUM(quantity)by category,DATE_TRUNC('month', order_ts), and ASIN. Ranking raw line items would rank individual orders, not produc...