Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL for Repeat Customers

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

Your question is SQL for Repeat Customers. Start with the requirements and the two 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

Affirm wants to identify customers who completed multiple purchases through Affirm Checkout during the 12 months ending 2025-01-15. Write a PostgreSQL query that returns one row per qualifying customer.

Requirements

  1. Include only purchases with status = 'completed' from 2024-01-15 through 2025-01-15, inclusive.
  2. Return the customer ID, customer name, number of completed purchases, total purchase amount, and most recent purchase date.
  3. Include only customers with at least two qualifying purchases.
  4. Sort by purchase count descending, total purchase amount descending, and customer ID ascending.

Schema

customers
ColumnTypeDescription
customer_idPKINTEGERUnique customer identifier
customer_nameVARCHAR(100)Customer's display name
emailVARCHAR(255)Customer email address
purchases
ColumnTypeDescription
purchase_idPKINTEGERUnique purchase identifier
customer_idINTEGERCustomer associated with the purchase
purchase_dateDATEDate the purchase was submitted
amountNUMERIC(10,2)Purchase amount
statusVARCHAR(20)Purchase processing status
Tablescustomerspurchases
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results