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 Rolling Retention by Cohort
00:00
5 left

SQL Rolling Retention by Cohort

HardSQL · PostgreSQL

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
Interviewer

Your question is SQL Rolling Retention by Cohort. 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
Sign up free to run your codeLog inLn 1
Run your query to see results here.