Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Window Ranking for Customers
00:00
5 left

Window Ranking for Customers

MediumSQL · PostgreSQL

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
Interviewer

Your question is Window Ranking for Customers. 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
Sign up free to run your codeLog inLn 1
Run your query to see results here.