Compute conversion lift and a pooled two-proportion z-score for an A/B test in SQL

Quick Overview

Write a SQL query that summarizes an A/B test from raw exposure rows: exposures, conversions and conversion rate per variant, plus absolute lift, relative lift and a pooled two-proportion z-score for treatment. It tests conditional aggregation, comparing two groups in one query, numeric precision and division-by-zero guards.

Compute conversion lift and a pooled two-proportion z-score for an A/B test in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

The `experiment_events` table logs an A/B test with one row per exposure. For each `variant`, return the number of exposures, the number of conversions and the conversion rate. On the treatment row, also return the absolute lift and relative lift against control and the pooled two-proportion z-score. ```hint Comparing two rows The lift and the z-score need the control and treatment numbers at the same time. Think about how to get both variants' counts onto one row before you compute anything that compares them. ``` ### Input Table(s) `experiment_events` | Column | Type | Meaning | |---|---|---| | event_id | INT | One exposure. Primary key. | | user_id | INT | The exposed user. Not used for deduplication: the analysis counts exposures. | | variant | VARCHAR(20) | `'control'` or `'treatment'` | | converted | INT | 1 if the exposure converted, 0 if not | ### Output Contract One row per variant with these columns, in this order: | Column | Definition | |---|---| | variant | `'control'` or `'treatment'` | | n | Number of exposures (rows) for the variant | | conversions | Number of rows with `converted = 1` | | conversion_rate_pct | `100 * conversions / n`, rounded to 2 decimals | | absolute_lift_pct | Treatment row: `100 * (p_t - p_c)` in percentage points, rounded to 2 decimals. Control row: 0. | | relative_lift_pct | Treatment row: `100 * (p_t - p_c) / p_c`, rounded to 2 decimals. Control row: 0. | | z_score | Treatment row: the pooled z-score below, rounded to 4 decimals. Control row: 0. | Here $p_c = x_c / n_c$ and $p_t = x_t / n_t$ are the control and treatment conversion rates, with $x$ the conversions and $n$ the exposures. The z-score is $$z = \frac{p_t - p_c}{\sqrt{p_{pool}(1 - p_{pool})\left(\frac{1}{n_c} + \frac{1}{n_t}\right)}}, \qquad p_{pool} = \frac{x_c + x_t}{n_c + n_t}$$ Compute every value from unrounded rates and round only the final columns. Order the rows by `variant` ascending, so control comes first. ### Example `experiment_events` | event_id | user_id | variant | converted | |---|---|---|---| | 1 | 101 | control | 1 | | 2 | 102 | control | 0 | | 3 | 103 | control | 0 | | 4 | 104 | control | 0 | | 5 | 105 | treatment | 1 | | 6 | 106 | treatment | 1 | | 7 | 107 | treatment | 0 | | 8 | 108 | treatment | 0 | Expected result: | variant | n | conversions | conversion_rate_pct | absolute_lift_pct | relative_lift_pct | z_score | |---|---|---|---|---|---|---| | control | 4 | 1 | 25.00 | 0 | 0 | 0 | | treatment | 4 | 2 | 50.00 | 25.00 | 100.00 | 0.7303 | Control converts 1 of 4 (25.00%) and treatment 2 of 4 (50.00%). The absolute lift is 25.00 points and the relative lift is 25 / 25 = 100.00%. The pooled rate is 3 / 8 = 0.375, the standard error is $\sqrt{0.375 \cdot 0.625 \cdot 0.5} \approx 0.3423$, and z is 0.25 / 0.3423 = 0.7303. ### Constraints and Clarifications - `variant` is always `'control'` or `'treatment'`, and each variant has at least one row, so both output rows always exist. - `converted` is always 0 or 1 and never NULL. - The table has 1 to 10,000 rows. - A user may appear on more than one row. Count rows, not distinct users. - If the pooled standard error is 0 (every exposure converted, or none did), the treatment `z_score` is NULL. - If the control conversion rate is 0, the treatment `relative_lift_pct` is NULL, because the ratio is undefined. - The control row always reports 0 in the three comparison columns.

Overview: Write a SQL query that summarizes an A/B test from raw exposure rows: exposures, conversions and conversion rate per variant, plus absolute lift, relative lift and a pooled two-proportion z-score for treatment. It tests conditional aggregation, comparing two groups in one query, numeric precision and division-by-zero guards.

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

The experiment_events table logs an A/B test with one row per exposure. For each variant, return the number of exposures, the number of conversions and the conversion rate. On the treatment row, also return the absolute lift and relative lift against control and the pooled two-proportion z-score.

Input Table(s)

experiment_events

ColumnTypeMeaning
event_idINTOne exposure. Primary key.
user_idINTThe exposed user. Not used for deduplication: the analysis counts exposures.
variantVARCHAR(20)'control' or 'treatment'
convertedINT1 if the exposure converted, 0 if not

Output Contract

One row per variant with these columns, in this order:

ColumnDefinition
variant'control' or 'treatment'
nNumber of exposures (rows) for the variant
conversionsNumber of rows with converted = 1
conversion_rate_pct100 * conversions / n, rounded to 2 decimals
absolute_lift_pctTreatment row: 100 * (p_t - p_c) in percentage points, rounded to 2 decimals. Control row: 0.
relative_lift_pctTreatment row: 100 * (p_t - p_c) / p_c, rounded to 2 decimals. Control row: 0.
z_scoreTreatment row: the pooled z-score below, rounded to 4 decimals. Control row: 0.

Here pc=xc/ncp_c = x_c / n_c and pt=xt/ntp_t = x_t / n_t are the control and treatment conversion rates, with xx the conversions and nn the exposures. The z-score is

z=pt−pcppool(1−ppool)(1nc+1nt),ppool=xc+xtnc+ntz = \frac{p_t - p_c}{\sqrt{p_{pool}(1 - p_{pool})\left(\frac{1}{n_c} + \frac{1}{n_t}\right)}}, \qquad p_{pool} = \frac{x_c + x_t}{n_c + n_t}

Compute every value from unrounded rates and round only the final columns. Order the rows by variant ascending, so control comes first.

Example

experiment_events

event_iduser_idvariantconverted
1101control1
2102control0
3103control0
4104control0
5105treatment1
6106treatment1
7107treatment0
8108treatment0

Expected result:

variantnconversionsconversion_rate_pctabsolute_lift_pctrelative_lift_pctz_score
control4125.00000
treatment4250.0025.00100.000.7303

Control converts 1 of 4 (25.00%) and treatment 2 of 4 (50.00%). The absolute lift is 25.00 points and the relative lift is 25 / 25 = 100.00%. The pooled rate is 3 / 8 = 0.375, the standard error is 0.375⋅0.625⋅0.5≈0.3423\sqrt{0.375 \cdot 0.625 \cdot 0.5} \approx 0.3423, and z is 0.25 / 0.3423 = 0.7303.

Constraints and Clarifications

  • variant is always 'control' or 'treatment' , and each variant has at least one row, so both output rows always exist.
  • converted is always 0 or 1 and never NULL.
  • The table has 1 to 10,000 rows.
  • A user may appear on more than one row. Count rows, not distinct users.
  • If the pooled standard error is 0 (every exposure converted, or none did), the treatment z_score is NULL.
  • If the control conversion rate is 0, the treatment relative_lift_pct is NULL, because the ratio is undefined.
  • The control row always reports 0 in the three comparison columns.