Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Weekly Retention by Channel

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

Your question is SQL Weekly Retention by Channel. 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

Write a SQL query to calculate weekly retention for users acquired from each marketing channel.

Use the supplied user and activity records. Exclude users whose acquisition channel is unknown, include cohorts through the latest valid activity week, and represent weeks with no returning users as zero retention.

Output

  1. One row per acquisition channel, cohort week, and retention week.
  2. Columns: acquisition_channel, cohort_week, retention_week, cohort_users, retained_users, and retention_rate.
  3. Sort by channel, cohort week, and retention week ascending. retention_rate is a percentage rounded to two decimals.

Schema

users
ColumnTypeDescription
user_idPKINTUnique user identifier
acquisition_channelVARCHAR(50)Marketing channel credited with acquiring the user
acquisition_dateDATEDate the user was acquired
user_activity
ColumnTypeDescription
activity_idPKINTUnique activity event identifier
user_idINTUser associated with the activity event
activity_atTIMESTAMPTimestamp when the user was active
Tablesusersuser_activity
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results