Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
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
Sign up free to run your codeLog inLn 1
Run your query to see results here.