Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
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 feedbackSuccess 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
Top Customers by Net Sales
00:00
5 left

Top Customers by Net Sales

MediumSQL · PostgreSQL

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
Interviewer

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