Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Top 3 Revenue Per Region

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

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

Write a SQL query to rank customers by revenue within each region and return the top 3 per region.

Calculate revenue from all matching sales orders. Null revenue values should contribute zero. Customers tied at the same revenue must share a rank, and include every customer whose rank is 3 or better.

Output

  1. One row per qualifying customer with region, customer_id, customer_name, total_revenue, and revenue_rank.
  2. Sort by region, rank ascending, and customer_id ascending.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(150)Customer name
regionVARCHAR(50)Sales region assigned to the customer
sales_orders
ColumnTypeDescription
sales_order_idPKINTUnique sales order identifier
customer_idINTCustomer associated with the sales order
order_dateDATEDate the order was recorded
revenueDECIMAL(12,2)Revenue attributed to the order
Tablescustomerssales_orders
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results