Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top 3 Customers Per Region

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

Your question is Top 3 Customers Per Region. 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

OpenText wants a regional customer-spending view for account planning. Write a PostgreSQL query that identifies the three customers with the highest qualifying spend in each region during the year ending 2026-08-29.

Requirements

  1. Join customers to regions and orders.
  2. Include only orders from the past year, using CURRENT_DATE - INTERVAL '1 year' as the date boundary.
  3. Aggregate order amounts by customer and region, treating customers with no qualifying orders as spending zero.
  4. Rank customers independently within each region and return at most three per region.
  5. Sort the final output by region name, rank, and customer ID. Use customer ID as a deterministic tie-breaker.

Schema

regions
ColumnTypeDescription
region_idPKINTUnique region identifier
region_nameVARCHAR(50)Display name of the region
customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer display name
region_idINTAssigned region identifier
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
customer_idINTCustomer who placed the order
order_dateDATEDate the order was placed
order_amountNUMERIC(12,2)Monetary value of the order
Tablesregionscustomersorders
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results