Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Customers by Net Sales

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

Your question is Top Customers by Net Sales. 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

Business Context

You’re a data engineer at a large e-commerce marketplace (millions of monthly active buyers, tens of millions of orders per month). Finance and Growth teams use a weekly dashboard to identify the highest-value customers for VIP perks and retention campaigns. The dashboard must rank customers by net sales (paid revenue minus refunds) and break ties deterministically.

The data model is normalized: customer profiles live in customers, orders live in orders, and refunds are recorded at the order level in refunds. Not every order has a refund, and some refunds may be partial.

Task

Write a SQL query to return the top 10 customers by net sales across all time.

Requirements

  1. Compute gross_sales as the sum of orders.total_amount for orders with status = 'PAID'.
  2. Compute refund_amount as the sum of refunds.refund_amount for refunds with refund_status = 'COMPLETED'.
  3. Compute net_sales = gross_sales - refund_amount.
  4. Return: customer_id, full_name, gross_sales, refund_amount, net_sales.
  5. Rank customers by net_sales descending; break ties by customer_id ascending.
  6. Return only the top 10 customers.

Schema

customers
ColumnTypeDescription
customer_idPKINTPrimary key
full_nameVARCHAR(200)Customer display name; not null
created_atTIMESTAMPAccount creation timestamp; not null
orders
ColumnTypeDescription
order_idPKBIGINTPrimary key
customer_idINTForeign key to customers.customer_id; not null
order_dateDATEDate order was placed; not null
statusVARCHAR(20)Order status (PAID, CANCELLED, etc.); not null
total_amountDECIMAL(12,2)Total charged amount in USD; not null
refunds
ColumnTypeDescription
refund_idPKBIGINTPrimary key
order_idBIGINTForeign key to orders.order_id; not null
refund_dateDATEDate refund was issued; not null
refund_statusVARCHAR(20)Refund status (COMPLETED, PENDING, etc.); not null
refund_amountDECIMAL(12,2)Amount refunded in USD; not null
Tablescustomersordersrefunds
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results