Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Top 3 Per Region

HardSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is SQL Top 3 Per Region. Start with the requirements and the four tables on the right.

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.

Problem

Business Context

Henry Schein wants a regional view of product demand to support inventory planning across its distribution network. Use completed sales orders from the last completed quarter, defined here as Q4 2025.

Task

Write a PostgreSQL query that returns the top three products by total units sold within each region from October 1 through December 31, 2025.

Requirements

  1. Include only orders with status = 'completed' and order dates within Q4 2025, including both boundary dates.
  2. Join orders to regions, order items, and products.
  3. Aggregate total units by region and product, treating a NULL quantity as zero.
  4. Rank products independently within each region using a window function.
  5. Return products with ranks 1 through 3, ordered by region, rank, and product name.

Schema

regions
ColumnTypeDescription
region_idPKINTUnique region identifier
region_nameVARCHAR(50)Distribution region name
sales_orders
ColumnTypeDescription
order_idPKINTUnique sales order identifier
region_idINTRegion associated with the order
order_dateDATEDate the order was placed
statusVARCHAR(20)Order processing status
sales_order_items
ColumnTypeDescription
order_item_idPKINTUnique order line identifier
order_idINTRelated sales order identifier
product_idINTProduct sold on the order line
quantityINTNumber of units sold
products
ColumnTypeDescription
product_idPKINTUnique product identifier
product_nameVARCHAR(100)Product display name
product_skuVARCHAR(30)Stock keeping unit
Tablesregionssales_orderssales_order_itemsproducts
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results