Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Calculate 30-Day User Retention

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

Your question is Calculate 30-Day User Retention. Start with the requirements and the three 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

PulseBoard wants to measure whether newly active users come back within 30 days of their first login. Write a SQL query to calculate 30-day retention by signup cohort date.

A user is considered retained if they have at least one feature usage event between 1 and 30 days after their first login date. Ignore usage on the same day as first login.

Requirements

  1. Find each user's first login date from the user_logins table.
  2. Join to feature_usage and determine whether the user had any usage event from first_login_date + 1 day through first_login_date + 30 days.
  3. Return one row per signup_date from the users table with:
    • signup_date
    • cohort_size
    • retained_users
    • retention_rate rounded to 4 decimal places
  4. Include cohorts even if retention is 0, and order results by signup_date ascending.

Schema

users
ColumnTypeDescription
user_idPKINTUnique user identifier
user_nameVARCHAR(100)User full name
signup_dateDATEDate the user signed up
plan_typeVARCHAR(20)Subscription plan at signup
user_logins
ColumnTypeDescription
login_idPKINTUnique login event identifier
user_idINTUser who logged in
login_dateDATEDate of the login event
device_typeVARCHAR(20)Device used for login
feature_usage
ColumnTypeDescription
usage_idPKINTUnique feature usage event identifier
user_idINTUser who used a feature
usage_dateDATEDate of feature usage
feature_nameVARCHAR(50)Name of the feature used
Tablesusersuser_loginsfeature_usage
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results