Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Top Customers by Monthly Spend
00:00
5 left

Top Customers by Monthly Spend

MediumSQL · PostgreSQL

Problem

Write an SQL query to find the top 3 customers by total spend each month.

Use the provided customer and order data. Include only customers with orders, and treat each order amount as spend. If customers tie, lower customer_id ranks first.

Output

  1. One row per selected customer and month
  2. Columns: month_start, customer_name, total_spend, and spend_rank
  3. Include up to three customers per month, ordered by month_start, spend_rank, and customer_id

Schema

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

Your question is Top Customers by Monthly Spend. 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.