Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL Retention by Channel
00:00
5 left

SQL Retention by Channel

MediumSQL · PostgreSQL

Problem

Write a SQL query to calculate the month-over-month retention rate of users acquired through different marketing channels.

Use the users and user_activity tables. A retained user is an acquired user with at least one activity event during the calendar month immediately following acquisition.

Output

  1. One row per non-null acquisition channel and acquisition month with at least one acquired user.
  2. Columns: acquisition_channel, acquisition_month, acquired_users, retained_users, and retention_rate.
  3. Return retention_rate as a percentage rounded to two decimal places, ordered by acquisition month and channel.

Schema

users
ColumnTypeDescription
user_idPKINTUnique user identifier
acquired_atTIMESTAMPTimestamp when the user was acquired
acquisition_channelVARCHAR(50)Marketing channel that acquired the user
user_activity
ColumnTypeDescription
activity_idPKINTUnique activity event identifier
user_idINTUser associated with the activity event
activity_atTIMESTAMPTimestamp of the user activity event
Tablesusersuser_activity
Interviewer

Your question is SQL Retention by Channel. Start with the requirements and the two 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.