Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling Retention Rates by Cohort

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

Your question is Rolling Retention Rates by Cohort. 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

The OpenAI ChatGPT analytics team wants to compare engagement across user signup cohorts. Write a PostgreSQL query that measures activity during each user's first ninety days after signup.

Requirements

  1. Group users by their signup_date cohort.
  2. Calculate daily retention as the number of distinct users active on each cohort date divided by the cohort size.
  3. Calculate the ninety-day rolling average of daily retention for each cohort using a window function ordered by activity date.
  4. Exclude activity before signup, activity after day 90, users without a signup date, and rows with a null activity date. Return results ordered by cohort and activity date.

Schema

users
ColumnTypeDescription
user_idPKINTEGERUnique user identifier
signup_dateDATEDate the user signed up
user_activity
ColumnTypeDescription
activity_idPKINTEGERUnique activity record
user_idINTEGERUser associated with the activity
activity_dateDATEDate of the activity
Tablesusersuser_activity
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results