Build a Weekly Signup Cohort Retention Table in SQL

Last updated: November 29, 2026

Quick Overview

From a users table (user_id, signup_time) and an events table (user_id, event_name, event_time), produce a table of weekly signup cohorts showing the percentage of each cohort that performed a key action in week 0 through week 8 after signup.

Mixpanel
Data Manipulation (SQL/Python)
Data Engineer
Mixpanel
November 29, 2026
Data Engineer
Onsite
Data Manipulation (SQL/Python)
Medium

0

0

0 solved


From a users table (user_id, signup_time) and an events table (user_id, event_name, event_time), produce a table of weekly signup cohorts showing the percentage of each cohort that performed a key action in week 0 through week 8 after signup.

Retention is one of Mixpanel's flagship reports, and customers often ask why Mixpanel's numbers differ from a hand-written warehouse query. This question checks whether you can define retention buckets precisely and avoid the classic denominator and incomplete-week mistakes.

What the Interviewer Expects
  • Define week N relative to each user's signup time, not calendar weeks
  • Use cohort size from users table as denominator, including users who never return
  • Mask cells whose window has not fully elapsed
  • Count distinct users per cell
  • Explain the difference between 'on' and 'on or after' retention
Key Topics to Cover
cohort analysis
retention
date arithmetic
pivoting
denominators
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would you change this to 'unbounded' retention (did the action in week N or any later week)?
  • Why might this query disagree with Mixpanel's retention report for the same data?
  • How would you add a breakdown by acquisition channel without exploding row counts?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data

Key action is song_played as an example. Week N means floor((event_time - signup_time) / 7 days) = N, so each user has their own clock. Cohorts are grouped by the calendar week of signup. Week 0 i...

Approach
  1. Build cohorts: user, cohort week, signup time.
  2. Join qualifying events, compute week index, keep 0..8, deduplicate to (user, week).
  3. Count retained users per (cohort, week) and divide by cohort...

Submit Your Answer
Markdown supported

Related Questions