Top 3 Products per Category by Quantity Sold in the Last 30 Days

Quick Overview

Write a SQL query that finds the three best-selling products in each category by units sold over a fixed 30-day window. It tests joins, date-range filtering with inclusive bounds, grouped aggregation, and per-group ranking with a deterministic tie rule.

Top 3 Products per Category by Quantity Sold in the Last 30 Days

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

For each `category`, find the top 3 products by total `quantity` sold in the last 30 days relative to the fixed reference date `2024-06-30`. Return `category`, `product_id`, `product_name`, `total_quantity`. ```hint Pin down the window Write out the first and last calendar dates that count as "the last 30 days" before you write the filter, and check both ends against the reference date. ``` ### Input Table(s) Table: `Products` | Column | Type | Meaning | | --- | --- | --- | | product_id | INT | Identifier of the product. Primary key. | | product_name | VARCHAR(100) | Display name of the product. | | category | VARCHAR(50) | Category the product belongs to. | Table: `Sales` | Column | Type | Meaning | | --- | --- | --- | | sale_id | INT | Identifier of the sale. Primary key. | | product_id | INT | Product sold. Foreign key to `Products.product_id`. | | quantity | INT | Units sold in this sale. | | sale_date | DATE | Date of the sale. | Keys: `Products.product_id` and `Sales.sale_id` are primary keys. A product can have any number of sales, including several on the same day. ### Output Contract - Columns, in this order: `category`, `product_id`, `product_name`, `total_quantity`. - The window is the 30 days ending on the reference date: `sale_date` from `2024-06-01` through `2024-06-30`, both ends inclusive. - `total_quantity` is the sum of `quantity` over a product's sales inside the window. - Within each category, rank products by `total_quantity` descending, with ties broken by smaller `product_id`. Keep at most the first 3. - Order rows by `category` ascending, then by that rank within the category. Because `product_id` is unique, this ordering is unique. - If no sales fall in the window, return an empty result. ### Example `Products` | product_id | product_name | category | | --- | --- | --- | | 1 | Apple | Fruit | | 2 | Banana | Fruit | | 3 | Cherry | Fruit | | 4 | Mango | Fruit | | 5 | Milk | Dairy | | 6 | Cheese | Dairy | | 7 | Yogurt | Dairy | `Sales` | sale_id | product_id | quantity | sale_date | | --- | --- | --- | --- | | 1 | 1 | 5 | 2024-06-10 | | 2 | 2 | 8 | 2024-06-15 | | 3 | 3 | 3 | 2024-06-20 | | 4 | 3 | 5 | 2024-06-02 | | 5 | 4 | 6 | 2024-06-30 | | 6 | 1 | 9 | 2024-05-31 | | 7 | 5 | 4 | 2024-06-01 | | 8 | 6 | 2 | 2024-06-25 | | 9 | 7 | 10 | 2024-07-01 | Expected result | category | product_id | product_name | total_quantity | | --- | --- | --- | --- | | Dairy | 5 | Milk | 4 | | Dairy | 6 | Cheese | 2 | | Fruit | 2 | Banana | 8 | | Fruit | 3 | Cherry | 8 | | Fruit | 4 | Mango | 6 | Apple's sale on `2024-05-31` falls outside the window, so Apple totals 5 and drops to fourth place in Fruit. Banana and Cherry tie at 8, and Banana comes first because its `product_id` is smaller. Yogurt's only sale is after the reference date, so Yogurt does not appear, and Dairy has just two rows. ### Constraints and Clarifications - There are between 1 and 10,000 products and between 1 and 100,000 sales. - `quantity` is a positive integer and `sale_date` is a valid `DATE`. - Sales after `2024-06-30` do not count, even if the table contains them. - Only products with at least one sale in the window are ranked. A category with fewer than 3 such products returns only those products, and a category with none returns no rows. - Several sales of the same product, on the same day or on different days, are summed into one row for that product. - If a product has a NULL `category`, those products are ranked together as one group, listed after every named category (PostgreSQL's default for ascending order).

Overview: Write a SQL query that finds the three best-selling products in each category by units sold over a fixed 30-day window. It tests joins, date-range filtering with inclusive bounds, grouped aggregation, and per-group ranking with a deterministic tie rule.

General logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

For each category, find the top 3 products by total quantity sold in the last 30 days relative to the fixed reference date 2024-06-30. Return category, product_id, product_name, total_quantity.

Input Table(s)

Table: Products

ColumnTypeMeaning
product_idINTIdentifier of the product. Primary key.
product_nameVARCHAR(100)Display name of the product.
categoryVARCHAR(50)Category the product belongs to.

Table: Sales

ColumnTypeMeaning
sale_idINTIdentifier of the sale. Primary key.
product_idINTProduct sold. Foreign key to Products.product_id.
quantityINTUnits sold in this sale.
sale_dateDATEDate of the sale.

Keys: Products.product_id and Sales.sale_id are primary keys. A product can have any number of sales, including several on the same day.

Output Contract

  • Columns, in this order: category , product_id , product_name , total_quantity .
  • The window is the 30 days ending on the reference date: sale_date from 2024-06-01 through 2024-06-30 , both ends inclusive.
  • total_quantity is the sum of quantity over a product's sales inside the window.
  • Within each category, rank products by total_quantity descending, with ties broken by smaller product_id . Keep at most the first 3.
  • Order rows by category ascending, then by that rank within the category. Because product_id is unique, this ordering is unique.
  • If no sales fall in the window, return an empty result.

Example

Products

product_idproduct_namecategory
1AppleFruit
2BananaFruit
3CherryFruit
4MangoFruit
5MilkDairy
6CheeseDairy
7YogurtDairy

Sales

sale_idproduct_idquantitysale_date
1152024-06-10
2282024-06-15
3332024-06-20
4352024-06-02
5462024-06-30
6192024-05-31
7542024-06-01
8622024-06-25
97102024-07-01

Expected result

categoryproduct_idproduct_nametotal_quantity
Dairy5Milk4
Dairy6Cheese2
Fruit2Banana8
Fruit3Cherry8
Fruit4Mango6

Apple's sale on 2024-05-31 falls outside the window, so Apple totals 5 and drops to fourth place in Fruit. Banana and Cherry tie at 8, and Banana comes first because its product_id is smaller. Yogurt's only sale is after the reference date, so Yogurt does not appear, and Dairy has just two rows.

Constraints and Clarifications

  • There are between 1 and 10,000 products and between 1 and 100,000 sales.
  • quantity is a positive integer and sale_date is a valid DATE .
  • Sales after 2024-06-30 do not count, even if the table contains them.
  • Only products with at least one sale in the window are ranked. A category with fewer than 3 such products returns only those products, and a category with none returns no rows.
  • Several sales of the same product, on the same day or on different days, are summed into one row for that product.
  • If a product has a NULL category , those products are ranked together as one group, listed after every named category (PostgreSQL's default for ascending order).