Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Window Ranking for Customers

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

Your question is Window Ranking for Customers. 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

Publicis Groupe México wants to identify the highest-value customers within each product category for audience planning and campaign prioritization. Write a PostgreSQL query using joins, aggregation, and a window function.

Requirements

  1. Consider only orders with order_status = 'completed'.
  2. Calculate each customer's total completed order value within each product category, treating NULL order amounts as zero.
  3. Rank customers independently within each category by total value in descending order.
  4. Return only the top three customers per category, ordered by category, rank, and customer ID.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer's full name
products
ColumnTypeDescription
product_idPKINTUnique product identifier
categoryVARCHAR(50)Product category
product_nameVARCHAR(100)Product name
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
customer_idINTCustomer who placed the order
product_idINTProduct included in the order
order_amountNUMERIC(10,2)Monetary value of the order
order_statusVARCHAR(20)Order state
Tablescustomersproductsorders
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results