Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
SQL Retention Cohorts with Windows
00:00
5 left

SQL Retention Cohorts with Windows

HardSQL · PostgreSQL

Problem

Write a query using SQL window functions to calculate running 30-day active user retention cohorts across different subscription tiers.

Use the provided users, subscriptions, and activity_events tables. Treat each user's signup month as the cohort, use the non-ended subscription at signup, and evaluate activity during signup day through day 30.

Output

  1. One row per subscription tier, signup cohort month, and checkpoint day 0, 7, 14, 21, 28, or 30.
  2. Return tier, cohort_month, days_since_signup, cohort_size, running_active_users, and retention_rate_pct.
  3. Include cohorts with no qualifying activity, order by tier, cohort_month, and days_since_signup.

Schema

users
ColumnTypeDescription
user_idPKINTUnique user identifier
signup_dateDATEUser signup date
subscriptions
ColumnTypeDescription
subscription_idPKINTUnique subscription identifier
user_idINTSubscribed user
tierVARCHAR(30)Subscription tier
started_atDATESubscription start date
ended_atDATESubscription end date, or NULL when active
activity_events
ColumnTypeDescription
event_idPKINTUnique activity event identifier
user_idINTUser generating the activity
activity_atTIMESTAMPTimestamp of user activity
event_typeVARCHAR(40)Activity event type
Tablesuserssubscriptionsactivity_events
Interviewer

Your question is SQL Retention Cohorts with Windows. Start with the requirements and the three 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.