Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

7-Day Rolling Retention SQL

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

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

Andela Products wants to monitor whether users return after their initial product activity. Write a PostgreSQL query that calculates the 7-day rolling retention rate for each login date.

For this question, define daily 7-day retention as the percentage of distinct users who logged in on a given date and logged in again during the following 1 through 7 calendar days, inclusive. A user may generate multiple login events on the same date, but should count only once per daily cohort.

Requirements

  1. Deduplicate each user's login events to one user-date row.
  2. Calculate the daily cohort size for every login date.
  3. Count distinct cohort users who returned within the next seven calendar days, including the seventh day.
  4. Return login_date, cohort_users, retained_users, and retention_rate, rounded to four decimal places.
  5. Sort the result chronologically and ignore login events with a null timestamp.

Schema

users
ColumnTypeDescription
user_idPKINTEGERUnique Andela Products user identifier
display_nameVARCHAR(100)User's display name
login_events
ColumnTypeDescription
event_idPKINTEGERUnique login event identifier
user_idINTEGERUser associated with the login event
login_atTIMESTAMPTimestamp when the user logged in
Tablesuserslogin_events
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results