Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Window Functions for Retention

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

Your question is Window Functions for Retention. 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

Ancestry wants to understand whether users who join in January 2024 continue using the Family Tree experience. Write a PostgreSQL query to calculate monthly retention for this signup cohort from January through March 2024.

Requirements

  1. Define the cohort as users whose signup_date falls in January 2024.
  2. Count each cohort user at most once per calendar month, even if they generate multiple Family Tree events.
  3. Use a window function to deduplicate monthly activity, then return active users and retention percentage for months 0, 1, and 2 relative to signup cohort month.
  4. Include months with zero activity and order results chronologically.

Schema

users
ColumnTypeDescription
user_idPKINTUnique Ancestry user identifier
signup_dateDATEDate the user created an Ancestry account
product_events
ColumnTypeDescription
event_idPKINTUnique product event identifier
user_idINTUser associated with the event
event_atTIMESTAMPTimestamp when the event occurred
surfaceVARCHAR(50)Ancestry product surface generating the event
Tablesusersproduct_events
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results