Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL: Returning Customers Streak

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

Your question is SQL: Returning Customers Streak. 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

Movate's customer engagement analytics team wants to identify returning customers who maintained purchasing activity across consecutive calendar months. Write a PostgreSQL query using the customers and purchases tables.

Requirements

  1. Treat multiple purchases by the same customer in one month as a single active month.
  2. Identify every customer streak containing at least three consecutive calendar months.
  3. Do not use LEAD() or LAG().
  4. Return the customer ID, customer name, streak start month, streak end month, and number of consecutive months.
  5. Exclude purchases that do not match a customer and sort by customer ID, then streak start month.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer display name
segmentVARCHAR(30)Customer business segment
purchases
ColumnTypeDescription
purchase_idPKINTUnique purchase identifier
customer_idINTCustomer associated with the purchase
purchase_dateDATEDate on which the purchase occurred
purchase_amountNUMERIC(10,2)Purchase amount
channelVARCHAR(30)Purchase channel
Tablescustomerspurchases
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results