Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top 5 Products by Revenue Per Region

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

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

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
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results