Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Rolling Retention by Cohort

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

Your question is SQL Rolling Retention 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

NVIDIA Omniverse needs a reliable cohort-retention report for user activity. Events can arrive after the activity occurred, so retention must be calculated from event_at, not the ingestion timestamp.

Write a PostgreSQL query that reports 7-day rolling retention by cohort. A user belongs to the cohort defined by the date of their earliest valid event. A user is retained when they have at least one later event from cohort day 1 through cohort day 7, inclusive.

Requirements

  1. Derive each user's cohort from the minimum non-null event_at across all events.
  2. Count each cohort's distinct users and distinct users retained within the seven-day window.
  3. Return cohort_date, cohort_size, retained_users, and retention_rate_pct, ordered by cohort date.
  4. Ignore received_at when determining cohort membership or retention, allowing late-arriving events to be counted correctly.

Schema

users
ColumnTypeDescription
user_idPKINTUnique user identifier
user_nameVARCHAR(100)NVIDIA customer or developer name
user_events
ColumnTypeDescription
event_idPKINTUnique event identifier
user_idINTUser associated with the event
event_atTIMESTAMPTZTimestamp when the activity occurred
received_atTIMESTAMPTZTimestamp when the event reached the warehouse
event_typeVARCHAR(50)Type of user activity
Tablesusersuser_events
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results