Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL Window Function Ranking
00:00
5 left

SQL Window Function Ranking

EasySQL · PostgreSQL

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
Interviewer

Your question is SQL Window Function Ranking. Start with the requirements and the three tables in the Question tab.

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.
CodePostgreSQL
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.