Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Join and Rank Top Products

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

Your question is Join and Rank Top Products. Start with the requirements and the two 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

Sigmoid Analytics wants to compare product performance across sales regions. Write a PostgreSQL query that joins product metadata with sales transactions and returns the highest-revenue products in each region.

Requirements

  1. Include only completed sales from calendar year 2025 with positive quantities and non-null unit prices.
  2. Calculate total revenue and units sold for each product and region.
  3. Rank products within each region by total revenue and return every product with a rank of 2 or better, preserving ties.
  4. Sort the final output by region, revenue rank, and product ID.

Schema

products
ColumnTypeDescription
product_idPKINTUnique product identifier
product_nameVARCHAR(100)Product name
categoryVARCHAR(50)Product category
sales
ColumnTypeDescription
sale_idPKINTUnique sale identifier
product_idINTProduct associated with the sale
regionVARCHAR(50)Sales region
quantityINTNumber of units sold
unit_priceNUMERIC(10,2)Price per unit
sale_dateDATEDate of the sale
statusVARCHAR(20)Sale processing status
Tablesproductssales
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results