Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Top 3 Customers by Month
00:00
5 left

Top 3 Customers by Month

MediumSQL · PostgreSQL

Problem

What SQL query would you use to find the top 3 customers by total spend for each month?

Use the provided customers and orders tables. Return exactly three customers per month, breaking spend ties by the lower customer_id.

Output

  1. One row per customer-month with month_start, customer_id, customer_name, and total_spend
  2. Include only the top three customers in each month
  3. Order by month_start ascending, total_spend descending, then customer_id ascending

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer display name
orders
ColumnTypeDescription
order_idPKINTUnique order identifier
customer_idINTCustomer associated with the order
order_dateDATEDate the order was placed
amountNUMERIC(12,2)Order spend amount
Tablescustomersorders
Interviewer

Your question is Top 3 Customers by Month. Start with the requirements and the two 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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.