Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling 30-Day Retention Query

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

Your question is Rolling 30-Day Retention Query. 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

EvenUp's marketing team wants to compare how effectively acquisition channels drive early customer engagement. Using customer acquisition records and subsequent activity events, calculate 30-day retention for each acquisition-date and channel cohort.

Assume a customer is retained when they generate at least one activity event strictly after acquisition and before 30 days have elapsed. Include cohorts with zero retained customers, and label missing acquisition channels as Unknown.

Requirements

  1. Return the acquisition date, acquisition channel, cohort size, retained customer count, and retention rate.
  2. Count each customer at most once, even if they have multiple activity events.
  3. Use a LEFT JOIN so cohorts with no retained customers remain in the output.
  4. Exclude events exactly 30 days after acquisition.
  5. Order results by acquisition channel and acquisition date.

Schema

customers
ColumnTypeDescription
customer_idPKINTEGERUnique customer identifier
acquired_atTIMESTAMPTZTimestamp when the customer was acquired
acquisition_channelVARCHAR(50)Marketing channel credited with acquisition
customer_events
ColumnTypeDescription
event_idPKINTEGERUnique activity event identifier
customer_idINTEGERCustomer associated with the event
event_atTIMESTAMPTZTimestamp when the activity occurred
event_typeVARCHAR(50)Type of customer activity
Tablescustomerscustomer_events
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results