Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
SQL Top 3 Per Region
00:00
5 left

SQL Top 3 Per Region

EasySQL · PostgreSQL

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
Interviewer

Your question is SQL Top 3 Per Region. Start with the requirements and the four 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
Sign up free to run your codeLog inLn 1
Run your query to see results here.