Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Top Customers by Last-Quarter Spend
00:00
5 left

Top Customers by Last-Quarter Spend

MediumSQL · PostgreSQL

Problem

Write a PostgreSQL query to find the top three customers by total spend in the last completed quarter by joining customers and transactions. Return customer_id, customer_name, and total_spend, ordered by highest spend first. Assume CURRENT_DATE = '2024-08-15', so the target quarter is 2024-04-01 to 2024-06-30.

Schema

ColumnTypeDescription
customer_id INT PK
customer_name VARCHAR(100)
segment VARCHAR(50)
onboarded_at DATE
ColumnTypeDescription
transaction_id INT PK
customer_id INT FK
transaction_date DATE
amount DECIMAL(10,2)
cost_center VARCHAR(50)

Representative Rows

customers
101Acme HealthEnterprise2023-11-10
102BluePeak RetailMid-Market2024-01-05
104Delta ManufacturingEnterprise2023-09-15
transactions
10021012024-04-01300.00SaaS
10061022024-05-181200.00SaaS
10101042024-06-251600.00Labor
Tablescustomerstransactions
Interviewer

Your question is Top Customers by Last-Quarter 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.