Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL for Monthly Retention
00:00
5 left

SQL for Monthly Retention

MediumSQL · PostgreSQL

Problem

Write a SQL query to calculate monthly retention for customers acquired through marketing campaigns.

Use the provided campaign, acquisition, customer, and activity data. Treat a customer as retained in a month when they have at least one activity during that calendar month on or after acquisition.

Output

  1. One row per campaign, acquisition cohort month, and activity month.
  2. Return campaign_id, cohort_month, activity_month, cohort_customers, retained_customers, and retention_rate.
  3. Include months from each cohort month through the latest activity month, including zero-retention months. Sort by campaign_id, cohort_month, and activity_month.

Schema

customers
ColumnTypeDescription
customer_idPKINTUnique customer identifier
customer_nameVARCHAR(100)Customer display name
campaigns
ColumnTypeDescription
campaign_idPKINTUnique marketing campaign identifier
campaign_nameVARCHAR(150)Marketing campaign name
channelVARCHAR(50)Campaign acquisition channel
campaign_acquisitions
ColumnTypeDescription
acquisition_idPKINTUnique acquisition record identifier
campaign_idINTReferenced marketing campaign
customer_idINTReferenced acquired customer
acquired_atTIMESTAMPTimestamp when the customer was acquired
customer_activity
ColumnTypeDescription
activity_idPKINTUnique activity identifier
customer_idINTCustomer performing the activity
activity_atTIMESTAMPTimestamp of customer activity
activity_typeVARCHAR(50)Type of customer activity
Tablescustomerscampaignscampaign_acquisitionscustomer_activity
Interviewer

Your question is SQL for Monthly Retention. Start with the requirements and the four 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.