Pooled standard error of the difference in two conversion rates in SQL
Quick Overview
Write a SQL query that computes the pooled conversion proportion and the pooled standard error of the difference between the control and treatment conversion rates in an A/B test table. It tests conditional aggregation, numeric division, and applying the two-proportion standard error formula correctly.
Pooled standard error of the difference in two conversion rates 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 or 0). Let $n_c$ and $n_t$ be the number of users in control and treatment, and $x_c$ and $x_t$ the number of conversions in each. Compute the pooled conversion proportion and the pooled standard error of the difference in proportions:
$$\text{pooled\_p} = \frac{x_c + x_t}{n_c + n_t}$$
$$\text{se\_diff} = \sqrt{\text{pooled\_p} \cdot (1 - \text{pooled\_p}) \cdot \left(\frac{1}{n_c} + \frac{1}{n_t}\right)}$$
Return one row with `pooled_p` and `se_diff`, each rounded to 4 decimals.
```hint Count first
The formula needs four numbers: the users and the conversions in each arm. One pass over the table can produce all four before any arithmetic happens.
```
### 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 |
|---|---|---|
| pooled_p | numeric | Total conversions over total users across both arms, rounded to 4 decimal places |
| se_diff | numeric | Pooled standard error from the formula above, rounded to 4 decimal places |
The result has a single row, so no ordering is needed.
### Example
Input `trials`:
| group_name | converted |
|---|---|
| control | 0 |
| treatment | 0 |
| treatment | 1 |
| control | 1 |
| treatment | 0 |
| control | 0 |
| treatment | 1 |
| treatment | 0 |
| treatment | 1 |
| treatment | 1 |
| control | 0 |
| control | 0 |
| control | 0 |
| treatment | 0 |
| treatment | 1 |
| control | 0 |
| control | 0 |
| control | 0 |
| control | 1 |
| treatment | 0 |
| treatment | 0 |
Expected output:
| pooled_p | se_diff |
|---|---|
| 0.3333 | 0.2060 |
Control has 2 conversions out of 10 users and treatment has 5 out of 11, so pooled_p = 7 / 21 = 0.3333. Then se_diff = sqrt(0.3333 × 0.6667 × (1/10 + 1/11)) = sqrt(0.042424) = 0.20597, which rounds to 0.2060.
### 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.
- Compute `se_diff` from the unrounded pooled proportion. Round only the two final outputs.
- Rounding uses half away from zero, which is what `ROUND` does on a `NUMERIC` value. A trailing zero does not matter: 0.2060 and 0.206 are the same value.
Overview: Write a SQL query that computes the pooled conversion proportion and the pooled standard error of the difference between the control and treatment conversion rates in an A/B test table. It tests conditional aggregation, numeric division, and applying the two-proportion standard error formula correctly.
Table trials 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 control and treatment, and xc and xt the number of conversions in each. Compute the pooled conversion proportion and the pooled standard error of the difference in proportions:
pooled_p=nc+ntxc+xt
se_diff=pooled_p⋅(1−pooled_p)⋅(nc1+nt1)
Return one row with pooled_p and se_diff, each rounded to 4 decimals.
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
pooled_p
numeric
Total conversions over total users across both arms, rounded to 4 decimal places
se_diff
numeric
Pooled standard error from the formula above, rounded to 4 decimal places
The result has a single row, so no ordering is needed.
Example
Input trials:
group_name
converted
control
0
treatment
0
treatment
1
control
1
treatment
0
control
0
treatment
1
treatment
0
treatment
1
treatment
1
control
0
control
0
control
0
treatment
0
treatment
1
control
0
control
0
control
0
control
1
treatment
0
treatment
0
Expected output:
pooled_p
se_diff
0.3333
0.2060
Control has 2 conversions out of 10 users and treatment has 5 out of 11, so pooled_p = 7 / 21 = 0.3333. Then se_diff = sqrt(0.3333 × 0.6667 × (1/10 + 1/11)) = sqrt(0.042424) = 0.20597, which rounds to 0.2060.
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.
Compute
se_diff
from the unrounded pooled proportion. Round only the two final outputs.
Rounding uses half away from zero, which is what
ROUND
does on a
NUMERIC
value. A trailing zero does not matter: 0.2060 and 0.206 are the same value.