Compute a two-proportion z-statistic step by step in SQL
Quick Overview
Write a SQL query that turns a per-user A/B test table into the control and treatment conversion rates, the pooled proportion, the pooled standard error and the two-proportion z-statistic in one row. It tests conditional aggregation, staged CTEs, numeric division and keeping full precision until the final rounding.
Compute a two-proportion z-statistic step by step in SQL
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
The `experiment` table has one row per user in an A/B test, with `group_name` (`'control'` or `'treatment'`) and `converted` (1 or 0). Let $n_c$ and $n_t$ be the number of users in each arm and $x_c$ and $x_t$ the number of conversions. Return a single row with these staged quantities, each rounded to 4 decimals:
- `p_control` $= x_c / n_c$ and `p_treatment` $= x_t / n_t$
- `pooled_p` $= (x_c + x_t) / (n_c + n_t)$
- `se` $= \sqrt{\text{pooled\_p} \cdot (1 - \text{pooled\_p}) \cdot (1/n_c + 1/n_t)}$
- `z_statistic` $= (\text{p\_treatment} - \text{p\_control}) / \text{se}$
```hint Build it in stages
Every output depends on the ones before it. Get the four counts once, then derive each quantity from them in order, and keep full precision until the final step.
```
### Input Table(s)
Table `experiment`:
| Column | Type | Meaning |
|---|---|---|
| group_name | VARCHAR(20) | Arm the user was assigned to: `'control'` or `'treatment'` |
| converted | SMALLINT | 1 if the user converted, 0 otherwise |
There is no key column. Each row is one user, and no user appears twice.
### Output Contract
Exactly one row with these columns, in this order:
| Column | Type | Meaning |
|---|---|---|
| p_control | numeric | Control conversion rate, rounded to 4 decimal places |
| p_treatment | numeric | Treatment conversion rate, rounded to 4 decimal places |
| pooled_p | numeric | All conversions over all users, rounded to 4 decimal places |
| se | numeric | Pooled standard error of the difference, rounded to 4 decimal places |
| z_statistic | numeric | (p_treatment minus p_control) divided by se, rounded to 4 decimal places |
The result has a single row, so no ordering is needed.
### Example
Input `experiment`:
| group_name | converted |
|---|---|
| treatment | 1 |
| treatment | 1 |
| treatment | 0 |
| treatment | 1 |
| control | 0 |
| control | 0 |
| control | 1 |
| control | 0 |
| treatment | 1 |
| control | 0 |
| control | 0 |
| control | 0 |
| treatment | 1 |
| control | 0 |
| treatment | 1 |
| control | 0 |
| control | 0 |
| treatment | 1 |
| treatment | 1 |
| treatment | 1 |
Expected output:
| p_control | p_treatment | pooled_p | se | z_statistic |
|---|---|---|---|---|
| 0.1000 | 0.9000 | 0.5000 | 0.2236 | 3.5777 |
Control has 1 conversion out of 10 users and treatment has 9 out of 10, so the rates are 0.1 and 0.9 and the pooled proportion is 10 / 20 = 0.5. The standard error is sqrt(0.5 × 0.5 × (1/10 + 1/10)) = sqrt(0.05) = 0.223607, and the z-statistic is 0.8 / 0.223607 = 3.577709.
### Constraints and Clarifications
- `group_name` is always `'control'` or `'treatment'`, and both arms have at least one user.
- `converted` is always 0 or 1 and is never NULL.
- Assume at least one user converted and at least one did not, so `se` is positive.
- A positive `z_statistic` means treatment converted at a higher rate than control.
- Compute every stage from unrounded values and round only the five outputs. Rounding uses half away from zero, which is what `ROUND` does on a `NUMERIC` value; trailing zeros do not matter (0.1000 equals 0.1).
Overview: Write a SQL query that turns a per-user A/B test table into the control and treatment conversion rates, the pooled proportion, the pooled standard error and the two-proportion z-statistic in one row. It tests conditional aggregation, staged CTEs, numeric division and keeping full precision until the final rounding.
The experiment table has one row per user in an A/B test, with group_name ('control' or 'treatment') and converted (1 or 0). Let nc and nt be the number of users in each arm and xc and xt the number of conversions. Return a single row with these staged quantities, each rounded to 4 decimals:
p_control=xc/nc
and
p_treatment=xt/nt
pooled_p=(xc+xt)/(nc+nt)
se=pooled_p⋅(1−pooled_p)⋅(1/nc+1/nt)
z_statistic=(p_treatment−p_control)/se
Input Table(s)
Table experiment:
Column
Type
Meaning
group_name
VARCHAR(20)
Arm the user was assigned to: 'control' or 'treatment'
converted
SMALLINT
1 if the user converted, 0 otherwise
There is no key column. Each row is one user, and no user appears twice.
Output Contract
Exactly one row with these columns, in this order:
Column
Type
Meaning
p_control
numeric
Control conversion rate, rounded to 4 decimal places
p_treatment
numeric
Treatment conversion rate, rounded to 4 decimal places
pooled_p
numeric
All conversions over all users, rounded to 4 decimal places
se
numeric
Pooled standard error of the difference, rounded to 4 decimal places
z_statistic
numeric
(p_treatment minus p_control) divided by se, rounded to 4 decimal places
The result has a single row, so no ordering is needed.
Example
Input experiment:
group_name
converted
treatment
1
treatment
1
treatment
0
treatment
1
control
0
control
0
control
1
control
0
treatment
1
control
0
control
0
control
0
treatment
1
control
0
treatment
1
control
0
control
0
treatment
1
treatment
1
treatment
1
Expected output:
p_control
p_treatment
pooled_p
se
z_statistic
0.1000
0.9000
0.5000
0.2236
3.5777
Control has 1 conversion out of 10 users and treatment has 9 out of 10, so the rates are 0.1 and 0.9 and the pooled proportion is 10 / 20 = 0.5. The standard error is sqrt(0.5 × 0.5 × (1/10 + 1/10)) = sqrt(0.05) = 0.223607, and the z-statistic is 0.8 / 0.223607 = 3.577709.
Constraints and Clarifications
group_name
is always
'control'
or
'treatment'
, and both arms have at least one user.
converted
is always 0 or 1 and is never NULL.
Assume at least one user converted and at least one did not, so
se
is positive.
A positive
z_statistic
means treatment converted at a higher rate than control.
Compute every stage from unrounded values and round only the five outputs. Rounding uses half away from zero, which is what
ROUND
does on a
NUMERIC
value; trailing zeros do not matter (0.1000 equals 0.1).