Trial Conversion and Day-30 Return Rates per Experiment Arm
SQL & DataModel answerLast 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_idBIGINTexperiment_idSTRINGvariantSTRING ('control'or'treatment')assigned_atTIMESTAMP (the first time the user was assigned; assignment is sticky)
trial_starts: one row per trial start
user_idBIGINTtrial_started_atTIMESTAMP
sessions: one row per user session
user_idBIGINTsession_start_atTIMESTAMP
Definitions
- Cohort: users whose
assigned_atfalls 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_atin 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:
| Column | Meaning |
|---|---|
variant | experiment arm |
assigned_users | distinct users in the cohort |
converted_users | distinct users who converted |
signup_rate | converted_users / assigned_users |
retained_users | distinct converted users who were retained |
retention_rate_among_converted | retained_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.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Treat the problem as a funnel in which each stage is a subset of the previous one, and make sure each stage has one row per user. Once that holds, COUNT(*) and COUNT(column) are distinct-user counts and no COUNT(DISTINCT ...) is needed to patch join fan-out.
- Assignment: pick one assignment row per user for the relevant experiment, then keep those assigned in January 2025.
- Conversion: join the cohort to
trial_startswith the 7-day window in the join condition, then takeMIN(trial_started_at)per user. This is the earliest qualifying trial. - Retention: for each converted user, test with
EXISTSwhether any session falls in[anchor + 30d, anchor + 37d). - Aggregation: left-join the converted and retained sets back to the cohort and group by variant.
Query
WITH first_assignment AS (
SELECT user_id, variant, assigned_at
FROM (
SELECT user_id, variant, assigned_at,
ROW_NUMBER() OVER (PARTITION BY user_id ORDER BY assigned_at) AS rn
FROM experiment_assignments
WHERE experiment_id = 'free_trial_1m' -- placeholder: state this assumption
) x
WHERE rn = 1
),
cohort AS (
SELECT user_id, variant, assigned_at
FROM first_assignment
WHERE assigned_at >= TIMESTAMP '2025-01-01 00:00:00'
AND assigned_at < TIMESTAMP '2025-02-01 00:00:00'
),
conversions AS (
SELECT c.user_id,
MIN(t.trial_started_at) AS trial_started_at
FROM cohort c
JOIN trial_starts t
ON t.user_id = c.user_id
AND t.trial_started_at >= c.assigned_at
AND t.trial_started_at < c.assigned_at + INTERVAL '7' DAY
GROUP BY c.user_id
),
retained AS (
SELECT cv.user_id
FROM conversions cv
WHERE EXISTS (
SELECT 1
FROM sessions s
WHERE s.user_id = cv.user_id
AND s.session_start_at >= cv.trial_started_at + INTERVAL '30' DAY
AND s.session_start_at < cv.trial_started_at + INTERVAL '37' DAY
)
)
SELECT
c.variant,
COUNT(*) AS assigned_users,
COUNT(cv.user_id) AS converted_users,
CAST(COUNT(cv.user_id) AS DOUBLE)
/ NULLIF(COUNT(*), 0) AS signup_rate,
COUNT(r.user_id) AS retained_users,
CAST(COUNT(r.user_id) AS DOUBLE)
/ NULLIF(COUNT(cv.user_id), 0) AS retention_rate_among_converted
FROM cohort c
LEFT JOIN conversions cv ON cv.user_id = c.user_id
LEFT JOIN retained r ON r.user_id = c.user_id
GROUP BY c.variant
ORDER BY c.variant;
Why this is correct
- One row per user at every stage.
first_assignmentis deduplicated byROW_NUMBER,conversionsis grouped byuser_id, andretainedis a filtered subset ofconversions. Both left joins are therefore one-to-at-most-one and cannot inflate counts. - Earliest qualifying trial. The window sits in the
JOINcondition, soMINonly sees eligible trials. A user with a trial before assignment, one on day 3 and one on day 20 is anchored on day 3. Filtering afterMINwould wrongly drop them. - Correct anchor for retention. The retention window uses that same qualifying trial, as the definition requires.
- Two denominators.
signup_ratedivides by every assigned user in the arm.retention_rate_among_converteddivides by converters only. Left-joining onto the cohort preserves non-converters for the first rate. EXISTSfor "any session". It stops at the first match and cannot multiply rows the way a plain join tosessionswould.
Complexity
Let A, T, S be the row counts of assignments, trials and sessions.
- Time: O(A log A) for the window sort, plus roughly O(A + T + S) for hash joins on
user_id. In a warehouse this is a handful of shuffles keyed onuser_id. - Space: O(A) for the per-user intermediate sets. Sessions are only probed.
- Performance tip: restrict
sessionstosession_start_at >= '2025-01-31'and< '2025-03-17', the widest range any retention window can reach. This enables partition pruning on large tables.
Edge cases to mention
- Inclusive end date. Use
< '2025-02-01'. Using<= '2025-01-31'against a timestamp drops almost all of Jan 31. - Meaning of "within 7 days". I used
[assigned_at, assigned_at + 7 days), consistent with the half-open retention window. Trials before assignment are excluded since the treatment cannot have caused them. Some teams use calendar days instead, so state your choice. - Experiment ID. The prompt does not give one. Without a filter, a user enrolled in several experiments appears several times. Say so and use a placeholder.
- Dedup before date filter. If a user was first assigned in December and a duplicate row appears in January, filtering first would wrongly admit them. Dedupe first, then filter.
- Data maturity. A user assigned Jan 31 who converts Feb 7 has a retention window ending Mar 16. Running the query earlier makes recent converters look unretained and biases retention downward. Wait, or restrict to users whose full window has elapsed.
- Division. Cast to a floating type to avoid integer division, and use
NULLIFto avoid divide-by-zero for an arm with no converters. - Dialect. The interval syntax above is ANSI/Trino. Use
DATEADD(day, 7, ...)on Snowflake or Redshift, andTIMESTAMP_ADD(..., INTERVAL 7 DAY)on BigQuery.
Common mistakes
- Joining
sessionsdirectly and counting rows, which inflates counts. - Computing
MIN(trial_started_at)before applying the 7-day filter. - Using the assignment time, not the trial start, as the retention anchor.
- Using only converters as the denominator for the signup rate.
Follow-up: computing lift
Wrap the result as per_variant and pivot it to get relative lift:
SELECT
MAX(CASE WHEN variant = 'treatment' THEN signup_rate END)
/ MAX(CASE WHEN variant = 'control' THEN signup_rate END) - 1 AS signup_lift
FROM per_variant;
The same pattern works for retention. In practice, also report a confidence interval, for example a two-proportion z-test for signup and a test conditional on conversion for retention. Remember that retention among converters is a post-treatment conditioning, so the two arms' converter populations may differ, and an intent-to-treat metric (retained users divided by all assigned users) is a useful companion.