Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Retention by Cohort SQL

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

Your question is Retention by Cohort 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

Yahoo Mail wants to measure how consistently users return after creating an account. Write a PostgreSQL query that calculates monthly retention for signup cohorts from January through March 2024.

Requirements

  1. Define each user's cohort as the month of signup_date.
  2. Treat a user as retained in a month when at least one Yahoo Mail activity event occurs during that calendar month.
  3. Return periods 0 through 3 for every cohort, including periods with no retained users.
  4. Report cohort size, retained users, and retention percentage. Round the percentage to one decimal place.
  5. Deduplicate multiple events from the same user within a month and sort by cohort month and period.

Schema

users
ColumnTypeDescription
user_idPKINTYahoo Mail user identifier
signup_dateDATEDate when the account was created
account_statusVARCHAR(20)Current account status
yahoo_mail_events
ColumnTypeDescription
event_idPKINTUnique activity event identifier
user_idINTUser who generated the event
event_dateDATEDate of the Yahoo Mail activity
event_typeVARCHAR(30)Type of activity event
Tablesusersyahoo_mail_events
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results