Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Top Products by Region

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

Your question is SQL Top Products by Region. Start with the requirements and the three 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

Roche's commercial analytics team needs a regional view of product performance. Assume the reporting date is 2025-07-15, so the last completed quarter is 2025-04-01 through 2025-06-30.

Write a PostgreSQL query to identify the top three Roche products by revenue in each region.

Requirements

  1. Join sales, products, and regions using their key relationships.
  2. Include only sales from the last completed quarter, using an inclusive start date and exclusive end date.
  3. Calculate revenue as quantity * unit_price and aggregate it by region and product.
  4. Rank products within each region by descending revenue with RANK(), retaining ties at the third rank.
  5. Return region, product, total revenue, and rank, ordered by region, rank, and product name.

Schema

sales
ColumnTypeDescription
sale_idPKINTUnique sales record identifier
product_idINTReferences products.product_id
region_idINTReferences regions.region_id
sale_dateDATEDate on which the sale was recorded
quantityINTNumber of units sold
unit_priceDECIMAL(12,2)Revenue per unit in reporting currency
products
ColumnTypeDescription
product_idPKINTUnique Roche product identifier
product_nameVARCHAR(100)Roche product name
regions
ColumnTypeDescription
region_idPKINTUnique commercial region identifier
region_nameVARCHAR(100)Commercial region name
Tablessalesproductsregions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results