Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top 3 Revenue by Region

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

Your question is Top 3 Revenue by 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

Newton School of Technology wants a regional revenue summary for its course enrollments. Write a PostgreSQL query that calculates the sum of the three highest valid order revenues within each region.

Requirements

  1. Join orders with regions using region_id.
  2. Rank orders independently within each region by revenue descending. Break revenue ties with the smaller order_id.
  3. Exclude orders with NULL revenue, keep only the top three orders per region, and return the regional sum.
  4. Sort the final output by summed revenue descending, then region name ascending.

Schema

regions
ColumnTypeDescription
region_idPKINTPrimary key for a Newton School operating region
region_nameVARCHAR(50)Name of the operating region
orders
ColumnTypeDescription
order_idPKINTPrimary key for an enrollment order
region_idINTRegion associated with the order
revenueDECIMAL(12,2)Revenue generated by the order
Tablesregionsorders
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results