Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rank Top Clients by Product Revenue

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

Your question is Rank Top Clients by Product Revenue. 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

At Northstar Commerce, the analytics team wants to identify the top-performing clients for each product based on completed sales. Write a SQL query to rank clients by total revenue within each product.

Requirements

  1. Use only completed orders placed in 2024.
  2. Calculate total revenue per product_name and client_name using quantity * unit_price.
  3. Rank clients within each product by revenue in descending order using a ranking function.
  4. Return only the top 2 ranked clients per product. If two clients tie, they should receive the same rank.

Schema

clients
ColumnTypeDescription
client_idPKINTUnique client identifier
client_nameVARCHAR(100)Client name
regionVARCHAR(50)Client sales region
products
ColumnTypeDescription
product_idPKINTUnique product identifier
product_nameVARCHAR(100)Product name
categoryVARCHAR(50)Product category
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
client_idINTReferences clients.client_id
product_idINTReferences products.product_id
order_dateDATEDate the order was placed
quantityINTNumber of units ordered
unit_priceDECIMAL(10,2)Price per unit
statusVARCHAR(20)Order status such as completed or cancelled
Tablesclientsproductsorders
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results