Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Window Functions for Top Categories

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

Your question is Window Functions for Top Categories. 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

QuickBooks Online analytics needs a regional view of completed product revenue. Write a PostgreSQL query that identifies the top three product categories by revenue within each sales region.

Requirements

  1. Join orders to products and regions.
  2. Include only completed orders placed during January 2025, using revenue defined as quantity * unit_price.
  3. Aggregate revenue by region and product category, then rank categories within each region using a window function.
  4. Return categories ranked 1 through 3, including ties, ordered by region and rank.

Schema

sales_orders
ColumnTypeDescription
order_idPKINTUnique order identifier
region_idINTReference to the sales region
product_idINTReference to the purchased product
quantityINTNumber of units ordered
unit_priceDECIMAL(10,2)Price per unit
order_statusVARCHAR(20)Current order status
order_dateDATEDate the order was placed
products
ColumnTypeDescription
product_idPKINTUnique product identifier
product_nameVARCHAR(100)Product display name
categoryVARCHAR(50)Product category
regions
ColumnTypeDescription
region_idPKINTUnique region identifier
region_nameVARCHAR(50)Sales region name
Tablessales_ordersproductsregions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results