KatInterview

Trial Conversion and Day-30 Return Rates per Experiment Arm

SQL & DataModel answer
MediumModel answerSQL & Datasqlab-testingcohort-analysis

Last updated

Write one SQL query comparing control and treatment on 7-day trial conversion and on a week-long return window starting 30 days after the trial begins.

A marketing team is running an A/B test on a promotion that gives new users a free one-month trial. You have been asked to compare the two arms on how many users start a trial and how many of those users come back later. All timestamps are in UTC.

Schema

experiment_assignments: one row per user assignment

  • user_id BIGINT
  • experiment_id STRING
  • variant STRING ('control' or 'treatment')
  • assigned_at TIMESTAMP (the first time the user was assigned; assignment is sticky)

trial_starts: one row per trial start

  • user_id BIGINT
  • trial_started_at TIMESTAMP

sessions: one row per user session

  • user_id BIGINT
  • session_start_at TIMESTAMP

Definitions

  • Cohort: users whose assigned_at falls between 2025-01-01 and 2025-01-31, inclusive.
  • Converted: the user has a trial start within 7 days after assigned_at.
  • Retained: among converted users, the user has at least one session with session_start_at in the window [trial_started_at + 30 days, trial_started_at + 37 days).

Task

Write a single SQL query that returns one row per variant with these columns:

ColumnMeaning
variantexperiment arm
assigned_usersdistinct users in the cohort
converted_usersdistinct users who converted
signup_rateconverted_users / assigned_users
retained_usersdistinct converted users who were retained
retention_rate_among_convertedretained_users / converted_users

Rules

  • Count distinct users, never raw events.
  • If a user has several trial starts, use the earliest one that falls inside the 7-day window after assignment. That trial is the anchor for the retention window.
  • A user may have many sessions. One qualifying session is enough to count them as retained.

In your answer, explain how you handled the date boundaries, join fan-out, and the two different denominators. Mention any assumptions you had to make, since the prompt does not name a specific experiment ID.

Hints

Hint 1

Think of the metrics as a funnel of nested user sets: assigned, then converted, then retained. Reduce each stage to at most one row per user before you aggregate, so counts are automatically distinct-user counts.

Hint 2

Put the 7-day condition inside the join to `trial_starts`, and only then take `MIN(trial_started_at)` per user. Taking the minimum over all trials first can discard users whose earliest trial was outside the window but who have a valid one inside it.

Hint 3

Use `EXISTS` (not a plain join) against `sessions` for the retention check, and left-join the converted and retained sets back onto the cohort so non-converters still count in the signup denominator. Remember that 'through Jan 31 inclusive' means `< '2025-02-01'` on a timestamp.