Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Window Function Ranking

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

Your question is SQL Window Function Ranking. 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

Accenture España wants to identify the highest-value customers in each sales region. Write a SQL query to rank customers by their total revenue within each region.

Requirements

  1. Aggregate revenue per region and customer_id.
  2. Rank customers within each region by total revenue, highest first.
  3. Use a window function to assign the rank.
  4. Return only customers with at least one completed order.

Schema

regions
ColumnTypeDescription
region_idPKINTPrimary key for the region
region_nameVARCHAR(100)Region name
customers
ColumnTypeDescription
customer_idPKINTPrimary key for the customer
customer_nameVARCHAR(100)Customer name
region_idINTRegion assigned to the customer
orders
ColumnTypeDescription
order_idPKINTPrimary key for the order
customer_idINTCustomer who placed the order
order_statusVARCHAR(20)Order status, such as completed or cancelled
order_amountDECIMAL(10,2)Revenue from the order
order_dateDATEOrder date
Tablesregionscustomersorders
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results