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
Top 5 Products by Revenue Per Region
00:00
5 left

Top 5 Products by Revenue Per Region

MediumSQL · PostgreSQL

Problem

SAP Labs wants a regional revenue view for products sold through its enterprise portfolio. Write a PostgreSQL query that identifies the five highest-revenue products within each sales region.

Requirements

  1. Join orders to customers, regions, order items, and products.
  2. Include only orders with status = 'COMPLETED' and positive quantities.
  3. Calculate revenue as quantity * COALESCE(unit_price, list_price) and aggregate it by region and product.
  4. Rank products independently within each region and return the top five, using product_id as the deterministic tie-breaker.
  5. Return regions in ascending order, with revenue descending within each region.

Representative Data

Completed orders contain multiple product lines, while cancelled and pending orders must be excluded. Some products have no qualifying sales, and one completed line has a NULL transaction price.

Schema

regions
ColumnTypeDescription
region_idPKINTUnique region identifier
region_nameVARCHAR(50)Sales region name
customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer name
region_idINTCustomer's sales region
products
ColumnTypeDescription
product_idPKINTUnique product identifier
product_nameVARCHAR(100)SAP product name
list_priceDECIMAL(12,2)Default product price
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
customer_idINTCustomer placing the order
statusVARCHAR(20)Order lifecycle status
order_items
ColumnTypeDescription
order_item_idPKINTUnique order-line identifier
order_idINTRelated order
product_idINTSold product
quantityINTUnits sold
unit_priceDECIMAL(12,2)Actual line-item price
Tablesregionscustomersproductsordersorder_items
Interviewer

Your question is Top 5 Products by Revenue Per Region. Start with the requirements and the five 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.