Run a pooled two-proportion z-test on A/B test data in SQL
Quick Overview
Write a SQL query that runs a pooled two-proportion z-test on a per-user A/B test table and returns the z-statistic with a flag for significance at the 1.96 threshold. It tests conditional aggregation, numeric division, sign conventions and using an absolute value for a two-sided decision.
Run a pooled two-proportion z-test on A/B test data in SQL
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Table `trials` has one row per user in an A/B test, with `group_name` (`'control'` or `'treatment'`) and `converted` (1/0). Run a pooled two-proportion z-test. Let $n_1, x_1$ be the users and conversions in control and $n_2, x_2$ the same for treatment, with rates $p_1 = x_1 / n_1$ (control) and $p_2 = x_2 / n_2$ (treatment):
$$p = \frac{x_1 + x_2}{n_1 + n_2}, \qquad SE = \sqrt{p (1 - p) \left(\frac{1}{n_1} + \frac{1}{n_2}\right)}, \qquad z = \frac{p_1 - p_2}{SE}$$
Return one row with `z_statistic` (z rounded to 4 decimals) and `significant` (1 when $|z| > 1.96$, else 0).
```hint Compute it once
Both outputs come from the same z. Compute it once at full precision, then derive the rounded value and the flag from that one number.
```
### Input Table(s)
Table `trials`:
| 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 |
|---|---|---|
| z_statistic | numeric | (control rate minus treatment rate) divided by the pooled SE, rounded to 4 decimal places |
| significant | integer | 1 when the absolute value of z is greater than 1.96, otherwise 0 |
The result has a single row, so no ordering is needed.
### Example
Input `trials`:
| group_name | converted |
|---|---|
| treatment | 0 |
| control | 0 |
| control | 1 |
| control | 0 |
| treatment | 1 |
| treatment | 1 |
| control | 0 |
| treatment | 1 |
| treatment | 1 |
| treatment | 0 |
| treatment | 0 |
| treatment | 1 |
| control | 1 |
| control | 1 |
| control | 1 |
| control | 0 |
| control | 0 |
| control | 1 |
| treatment | 1 |
| treatment | 0 |
| control | 0 |
| treatment | 1 |
Expected output:
| z_statistic | significant |
|---|---|
| -0.8563 | 0 |
Control has 5 conversions out of 11 users ($p_1$ = 0.4545) and treatment has 7 out of 11 ($p_2$ = 0.6364). The pooled proportion is 12 / 22 = 0.5455, the standard error is sqrt(0.5455 × 0.4545 × (2/11)) = 0.2123, and z = -0.1818 / 0.2123 = -0.8563. Its absolute value is below 1.96, so `significant` is 0.
### 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 the standard error is positive.
- z is control minus treatment, so it is negative when treatment converts at a higher rate.
- Decide `significant` from the unrounded z. The comparison is strict: a z of exactly 1.96 or -1.96 gives 0.
- Rounding uses half away from zero, which is what `ROUND` does on a `NUMERIC` value.
Overview: Write a SQL query that runs a pooled two-proportion z-test on a per-user A/B test table and returns the z-statistic with a flag for significance at the 1.96 threshold. It tests conditional aggregation, numeric division, sign conventions and using an absolute value for a two-sided decision.
Table trials has one row per user in an A/B test, with group_name ('control' or 'treatment') and converted (1/0). Run a pooled two-proportion z-test. Let n1,x1 be the users and conversions in control and n2,x2 the same for treatment, with rates p1=x1/n1 (control) and p2=x2/n2 (treatment):
Return one row with z_statistic (z rounded to 4 decimals) and significant (1 when ∣z∣>1.96, else 0).
Input Table(s)
Table trials:
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
z_statistic
numeric
(control rate minus treatment rate) divided by the pooled SE, rounded to 4 decimal places
significant
integer
1 when the absolute value of z is greater than 1.96, otherwise 0
The result has a single row, so no ordering is needed.
Example
Input trials:
group_name
converted
treatment
0
control
0
control
1
control
0
treatment
1
treatment
1
control
0
treatment
1
treatment
1
treatment
0
treatment
0
treatment
1
control
1
control
1
control
1
control
0
control
0
control
1
treatment
1
treatment
0
control
0
treatment
1
Expected output:
z_statistic
significant
-0.8563
0
Control has 5 conversions out of 11 users (p1 = 0.4545) and treatment has 7 out of 11 (p2 = 0.6364). The pooled proportion is 12 / 22 = 0.5455, the standard error is sqrt(0.5455 × 0.4545 × (2/11)) = 0.2123, and z = -0.1818 / 0.2123 = -0.8563. Its absolute value is below 1.96, so significant is 0.
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 the standard error is positive.
z is control minus treatment, so it is negative when treatment converts at a higher rate.
Decide
significant
from the unrounded z. The comparison is strict: a z of exactly 1.96 or -1.96 gives 0.
Rounding uses half away from zero, which is what
ROUND
does on a
NUMERIC
value.