Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Customers by Purchase Volume

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

Your question is Top Customers by Purchase Volume. 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

Amazon DSP needs a customer-level view of purchase activity to prioritize audience analysis and optimization. Write a PostgreSQL query to find the 10 customers with the highest completed purchase volume.

Requirements

  1. Count purchase volume as the sum of quantity from items belonging to orders with status = 'completed'.
  2. Include customers with no completed purchases, treating their volume as zero.
  3. Return the top 10 customers, ordered by purchase volume descending and customer_id ascending to break ties.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer display name
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
customer_idINTCustomer who placed the order; NULL for unattributed orders
statusVARCHAR(20)Order lifecycle status
order_items
ColumnTypeDescription
order_idPKINTOrder associated with the item record
quantityINTNumber of units in the order item
Tablescustomersordersorder_items
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results