Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top 3 Items Per Region Query

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

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

The Apexon Data & AI platform tracks high-volume user transactions across regions. Write a PostgreSQL query that identifies the three most purchased items in each region during the last 30 days.

Assume the query runs on 2026-08-29. Count purchased units from completed transactions only. For distributed execution, filter the large transactions table before joining dimension tables, and aggregate before applying the ranking window function.

Requirements

  1. Include transactions from the inclusive 30-day interval ending today, including today’s transactions.
  2. Exclude rows that are not completed or have a non-positive or NULL quantity.
  3. Aggregate total purchased units by region and item.
  4. Rank items independently within each region, breaking quantity ties by ascending item_id.
  5. Return no more than three rows per region, ordered by region and rank.

Schema

transactions
ColumnTypeDescription
transaction_idPKBIGINTUnique transaction identifier
user_idBIGINTReferences users.user_id
item_idBIGINTReferences items.item_id
quantityINTEGERNumber of units in the transaction
occurred_atTIMESTAMPTimestamp when the transaction occurred
statusVARCHAR(20)Transaction processing status
users
ColumnTypeDescription
user_idPKBIGINTUnique user identifier
regionVARCHAR(40)Geographic region assigned to the user
items
ColumnTypeDescription
item_idPKBIGINTUnique item identifier
item_nameVARCHAR(100)Display name of the purchased item
Tablestransactionsusersitems
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results