Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Top Parts Per Category
00:00
5 left

Top Parts Per Category

MediumSQL · PostgreSQL

Problem

Write a SQL query using window functions to find the top three selling automotive parts in each store category for the last quarter at Advance Auto Parts.

Use the provided store, part, and sales data. Treat the last quarter as the complete calendar quarter immediately before the current quarter. Return only parts with sales during that period.

Output

  1. One row per qualifying part, with store_category, part_id, part_name, total_units_sold, and sales_rank.
  2. Include the top three parts in each category, ranked by total units sold descending.
  3. Break ties by part_id ascending, and order the final results by store_category, sales_rank, and part_id.

Schema

stores
ColumnTypeDescription
store_idPKINTUnique store identifier
store_nameVARCHAR(100)Store name
store_categoryVARCHAR(50)Business category assigned to the store
parts
ColumnTypeDescription
part_idPKINTUnique automotive part identifier
part_nameVARCHAR(150)Automotive part description
sales
ColumnTypeDescription
sale_idPKINTUnique sales record identifier
store_idINTStore associated with the sale
part_idINTPart associated with the sale
sale_dateDATEDate of the sale
quantityINTNumber of units sold
Tablesstorespartssales
Interviewer

Your question is Top Parts Per Category. Start with the requirements and the three tables in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePostgreSQL
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.