Compute next-day retention rate for each first-activity cohort

Quick Overview

A SQL exercise that groups users into cohorts by the date of their first activity and reports each cohort's size and next-day retention rate. It tests per-user aggregation, outer joins that keep users who never returned, distinct counting, and decimal division with rounding.

Compute next-day retention rate for each first-activity cohort

Company: Plaid

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

A user's cohort is the date of their first activity in the `activity` table. For each cohort date, report how many users are in the cohort and what fraction of them had any activity on the next calendar day. ### Input Table(s) Table `activity`: | Column | Type | Meaning | |---|---|---| | user_id | INT | User who performed the activity | | activity_date | DATE | Calendar date of the activity | | activity_type | VARCHAR(20) | Kind of activity, such as `login` | Keys: the table has no primary key. A user can have several rows on the same date, and identical rows can repeat. ### Output Contract - Columns, in this order: `cohort_date`, `cohort_size`, `retention_rate`. - One row per distinct first-activity date. - `cohort_size` is the number of distinct users whose first activity falls on `cohort_date`. - `retention_rate` is the number of those users with at least one row on `cohort_date` plus 1 day, divided by `cohort_size`, as a fraction from 0 to 1 rounded to 2 decimal places. - Order by `cohort_date` ascending. Each cohort date appears once, so this order is unique. ### Example Input `activity`: | user_id | activity_date | activity_type | |---|---|---| | 1 | 2024-01-01 | login | | 1 | 2024-01-02 | login | | 2 | 2024-01-01 | login | | 3 | 2024-01-02 | login | | 3 | 2024-01-03 | login | | 4 | 2024-01-02 | login | Expected output: | cohort_date | cohort_size | retention_rate | |---|---|---| | 2024-01-01 | 2 | 0.50 | | 2024-01-02 | 2 | 0.50 | Users 1 and 2 first appear on 2024-01-01. User 1 is active on 2024-01-02 and user 2 is not, so the rate is 1 / 2 = 0.50. Users 3 and 4 first appear on 2024-01-02. User 3 is active on 2024-01-03 and user 4 is not, so that rate is also 0.50. User 1's activity on 2024-01-02 does not put user 1 in the 2024-01-02 cohort, because each user belongs only to the cohort of their first date. ### Constraints and Clarifications - The table has at least one row. - `user_id` and `activity_date` are never NULL. - Every `activity_type` counts, both for finding a user's first date and for deciding whether they came back. - Only activity on exactly `cohort_date` plus 1 day counts as retained. A second session on the cohort date itself, or a return two or more days later, does not. - Several rows for the same user on the next day still count that user once. - Every cohort appears in the output, including those where nobody came back. A user whose next day has no rows in the table counts as not retained, so a cohort on the latest date in the data gets 0.00. ```hint Keep everyone in the denominator Each user belongs to exactly one cohort. Once you have that mapping, make sure the users who never came back still reach the final count. ```

Overview: A SQL exercise that groups users into cohorts by the date of their first activity and reports each cohort's size and next-day retention rate. It tests per-user aggregation, outer joins that keep users who never returned, distinct counting, and decimal division with rounding.

Plaid logo
Plaid
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

A user's cohort is the date of their first activity in the activity table. For each cohort date, report how many users are in the cohort and what fraction of them had any activity on the next calendar day.

Input Table(s)

Table activity:

ColumnTypeMeaning
user_idINTUser who performed the activity
activity_dateDATECalendar date of the activity
activity_typeVARCHAR(20)Kind of activity, such as login

Keys: the table has no primary key. A user can have several rows on the same date, and identical rows can repeat.

Output Contract

  • Columns, in this order: cohort_date , cohort_size , retention_rate .
  • One row per distinct first-activity date.
  • cohort_size is the number of distinct users whose first activity falls on cohort_date .
  • retention_rate is the number of those users with at least one row on cohort_date plus 1 day, divided by cohort_size , as a fraction from 0 to 1 rounded to 2 decimal places.
  • Order by cohort_date ascending. Each cohort date appears once, so this order is unique.

Example

Input activity:

user_idactivity_dateactivity_type
12024-01-01login
12024-01-02login
22024-01-01login
32024-01-02login
32024-01-03login
42024-01-02login

Expected output:

cohort_datecohort_sizeretention_rate
2024-01-0120.50
2024-01-0220.50

Users 1 and 2 first appear on 2024-01-01. User 1 is active on 2024-01-02 and user 2 is not, so the rate is 1 / 2 = 0.50. Users 3 and 4 first appear on 2024-01-02. User 3 is active on 2024-01-03 and user 4 is not, so that rate is also 0.50. User 1's activity on 2024-01-02 does not put user 1 in the 2024-01-02 cohort, because each user belongs only to the cohort of their first date.

Constraints and Clarifications

  • The table has at least one row.
  • user_id and activity_date are never NULL.
  • Every activity_type counts, both for finding a user's first date and for deciding whether they came back.
  • Only activity on exactly cohort_date plus 1 day counts as retained. A second session on the cohort date itself, or a return two or more days later, does not.
  • Several rows for the same user on the next day still count that user once.
  • Every cohort appears in the output, including those where nobody came back. A user whose next day has no rows in the table counts as not retained, so a cohort on the latest date in the data gets 0.00.