Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Products Per Region Query

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

Your question is Top Products Per Region Query. 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

Apex Systems wants a quarterly view of product performance in its sales analytics reporting. Write a PostgreSQL query that identifies the highest-grossing products in each sales region for the last completed quarter, defined here as April 1, 2025 through June 30, 2025.

Requirements

  1. Join regions, orders, order_items, and products to calculate product revenue by region.
  2. Include only orders whose order_date falls within the specified quarter.
  3. Calculate gross revenue as quantity * unit_price and aggregate it by region and product.
  4. Use a window function to return all products whose rank is within the top three positions per region. Products tied at a qualifying rank must all be included.
  5. Return region_name, product_name, gross_revenue, and the ranking value, ordered by region and revenue descending.

Schema

regions
ColumnTypeDescription
region_idPKINTUnique sales region identifier
region_nameVARCHAR(50)Sales region name
region_managerVARCHAR(100)Assigned region manager
products
ColumnTypeDescription
product_idPKINTUnique product identifier
product_nameVARCHAR(100)Product name
categoryVARCHAR(50)Product category
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
region_idINTRegion associated with the order
order_dateDATEOrder placement date
order_items
ColumnTypeDescription
order_item_idPKINTUnique order line identifier
order_idINTParent order identifier
product_idINTSold product identifier
quantityINTUnits sold
unit_priceNUMERIC(10,2)Selling price per unit
Tablesregionsproductsordersorder_items
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results