Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top 3 Records Per Region

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

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

Lynker Technologies wants to identify the highest-value sales records within each operating region. Write a PostgreSQL query that ranks records independently within each known region and returns the top three.

Requirements

  1. Join sales records to regions to display the region name.
  2. Treat a NULL sales amount as zero when ranking, while preserving the original sales value in the output.
  3. Return no more than three records per region, ordered by sales descending and sale_id ascending as the tie-breaker.
  4. Exclude sales records whose region is not present in regions.

Schema

regions
ColumnTypeDescription
region_idPKINTUnique region identifier
region_nameVARCHAR(50)Display name of the operating region
sales_records
ColumnTypeDescription
sale_idPKINTUnique sales record identifier
region_idINTRegion associated with the sales record
record_labelVARCHAR(100)Short description of the sales record
sales_amountNUMERIC(12,2)Sales amount used for ranking
Tablesregionssales_records
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results