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
Window Functions for Top Categories
00:00
5 left

Window Functions for Top Categories

MediumSQL · PostgreSQL

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
Interviewer

Your question is Window Functions for Top Categories. Start with the requirements and the three 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.