Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

7-Day Rolling Retention SQL

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

Your question is 7-Day Rolling Retention SQL. Start with the requirements and the one table 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

Khan Academy wants to monitor whether learners return after using the platform. Using login events, calculate the seven-day rolling retention rate for each eligible login date.

A learner is retained for cohort date D if the learner has at least one additional login from D + 1 through D + 7, inclusive. Only include cohort dates with a complete seven-day observation window, ending at the latest login date in the table.

Requirements

  1. Deduplicate multiple login events from the same learner on the same calendar date.
  2. Exclude anonymous events where user_id is NULL.
  3. Return the cohort date, cohort size, retained learner count, and retention rate as a decimal rounded to four places.
  4. Preserve cohort dates with zero retained learners and order results chronologically.

Schema

login_events
ColumnTypeDescription
event_idPKINTUnique login event identifier
user_idINTKhan Academy learner identifier; NULL represents an anonymous event
logged_in_atTIMESTAMPTZTimestamp when the learner logged in
surfaceVARCHAR(30)Khan Academy surface used for the login
Tableslogin_events
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results