Per-Segment Two-Proportion Z-Test for an A/B Test in SQL
Quick Overview
Write a PostgreSQL query that runs a pooled two-proportion z-test separately for each user segment of an A/B test and flags which segments are significant at the 95% level. It tests grouped conditional aggregation, guarding against division by zero, and reading per-segment results with care.
Per-Segment Two-Proportion Z-Test for an A/B Test in SQL
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
The `trials` table holds one row per user in an A/B test. Each user has a `segment` (`'mobile'` or `'desktop'`), a `group_name` (`'control'` or `'treatment'`) and a `converted` flag that is 1 if the user converted and 0 otherwise. Run a pooled two-proportion z-test separately within each segment, comparing control with treatment, and return one row per segment with `segment`, `z_statistic` and `significant`.
Within one segment, with $n_c$ and $n_t$ users and $x_c$ and $x_t$ conversions in control and treatment:
$$
\hat p_c = \frac{x_c}{n_c}, \quad \hat p_t = \frac{x_t}{n_t}, \quad \hat p = \frac{x_c + x_t}{n_c + n_t}
$$
$$
z = \frac{\hat p_c - \hat p_t}{\sqrt{\hat p\,(1 - \hat p)\left(\frac{1}{n_c} + \frac{1}{n_t}\right)}}
$$
The pooled rate $\hat p$ and the group sizes all come from that segment's rows only.
```hint Plan for the degenerate segment
Work out each segment's counts before you write the formula, and decide what your division does when a segment's standard error is 0.
```
### Input Table(s)
Table `trials`:
| Column | Type | Meaning |
|---|---|---|
| `segment` | VARCHAR(20) | User segment, either `'mobile'` or `'desktop'` |
| `group_name` | VARCHAR(20) | Experiment arm, either `'control'` or `'treatment'` |
| `converted` | SMALLINT | 1 if the user converted, 0 if not |
The table has no key column. Each row is one user.
### Output Contract
One row per segment that appears in the table, with these columns in this order:
| Column | Type | Rule |
|---|---|---|
| `segment` | VARCHAR | The segment |
| `z_statistic` | NUMERIC | That segment's $z$ (control minus treatment), rounded to 4 decimal places, or NULL when it is undefined |
| `significant` | INTEGER | 1 when the absolute value of $z$ is greater than 1.96, else 0 |
Order the rows by `segment` ascending. Each segment appears once, so that order is unique. The significance decision uses the unrounded $z$.
### Example
Input `trials`:
| segment | group_name | converted |
|---|---|---|
| desktop | control | 0 |
| desktop | control | 0 |
| desktop | control | 0 |
| mobile | treatment | 0 |
| mobile | control | 0 |
| mobile | treatment | 1 |
| mobile | treatment | 1 |
| desktop | control | 0 |
| mobile | treatment | 1 |
| desktop | control | 0 |
| desktop | treatment | 0 |
| desktop | treatment | 0 |
| desktop | treatment | 0 |
| desktop | control | 1 |
| mobile | control | 0 |
| mobile | treatment | 0 |
| desktop | control | 1 |
| desktop | treatment | 0 |
| mobile | treatment | 0 |
| desktop | control | 1 |
| mobile | control | 0 |
| mobile | treatment | 0 |
| mobile | control | 0 |
| mobile | treatment | 1 |
| desktop | treatment | 0 |
Expected output:
| segment | z_statistic | significant |
|---|---|---|
| desktop | 1.5612 | 0 |
| mobile | -1.7321 | 0 |
Desktop has 8 control users with 3 conversions and 5 treatment users with none, so its pooled rate is 3/13 and $z = \tfrac{3}{8} / \sqrt{3/52} \approx 1.5612$. Mobile has 4 control users with no conversions and 8 treatment users with 4, so its pooled rate is 1/3 and $z = -0.5 / \sqrt{1/12} = -\sqrt{3} \approx -1.7321$. Neither absolute value exceeds 1.96.
### Constraints and Clarifications
- `segment` only takes `'mobile'` and `'desktop'`, `group_name` only takes `'control'` and `'treatment'`, and `converted` is always 0 or 1, never NULL.
- When a segment has no users in one arm, or its pooled rate is exactly 0 or 1 (so the standard error is 0), its `z_statistic` is NULL and `significant` is 0. The query must not raise a division error.
- $z$ is control minus treatment, so it is negative when treatment converts better in that segment. Keep that sign.
- The test is two-sided, and the comparison with 1.96 is strict.
- There is no user id, so every row counts as one user. Do not deduplicate identical rows.
Overview: Write a PostgreSQL query that runs a pooled two-proportion z-test separately for each user segment of an A/B test and flags which segments are significant at the 95% level. It tests grouped conditional aggregation, guarding against division by zero, and reading per-segment results with care.
The trials table holds one row per user in an A/B test. Each user has a segment ('mobile' or 'desktop'), a group_name ('control' or 'treatment') and a converted flag that is 1 if the user converted and 0 otherwise. Run a pooled two-proportion z-test separately within each segment, comparing control with treatment, and return one row per segment with segment, z_statistic and significant.
Within one segment, with nc and nt users and xc and xt conversions in control and treatment:
The pooled rate p^ and the group sizes all come from that segment's rows only.
Input Table(s)
Table trials:
Column
Type
Meaning
segment
VARCHAR(20)
User segment, either 'mobile' or 'desktop'
group_name
VARCHAR(20)
Experiment arm, either 'control' or 'treatment'
converted
SMALLINT
1 if the user converted, 0 if not
The table has no key column. Each row is one user.
Output Contract
One row per segment that appears in the table, with these columns in this order:
Column
Type
Rule
segment
VARCHAR
The segment
z_statistic
NUMERIC
That segment's z (control minus treatment), rounded to 4 decimal places, or NULL when it is undefined
significant
INTEGER
1 when the absolute value of z is greater than 1.96, else 0
Order the rows by segment ascending. Each segment appears once, so that order is unique. The significance decision uses the unrounded z.
Example
Input trials:
segment
group_name
converted
desktop
control
0
desktop
control
0
desktop
control
0
mobile
treatment
0
mobile
control
0
mobile
treatment
1
mobile
treatment
1
desktop
control
0
mobile
treatment
1
desktop
control
0
desktop
treatment
0
desktop
treatment
0
desktop
treatment
0
desktop
control
1
mobile
control
0
mobile
treatment
0
desktop
control
1
desktop
treatment
0
mobile
treatment
0
desktop
control
1
mobile
control
0
mobile
treatment
0
mobile
control
0
mobile
treatment
1
desktop
treatment
0
Expected output:
segment
z_statistic
significant
desktop
1.5612
0
mobile
-1.7321
0
Desktop has 8 control users with 3 conversions and 5 treatment users with none, so its pooled rate is 3/13 and z=83/3/52≈1.5612. Mobile has 4 control users with no conversions and 8 treatment users with 4, so its pooled rate is 1/3 and z=−0.5/1/12=−3≈−1.7321. Neither absolute value exceeds 1.96.
Constraints and Clarifications
segment
only takes
'mobile'
and
'desktop'
,
group_name
only takes
'control'
and
'treatment'
, and
converted
is always 0 or 1, never NULL.
When a segment has no users in one arm, or its pooled rate is exactly 0 or 1 (so the standard error is 0), its
z_statistic
is NULL and
significant
is 0. The query must not raise a division error.
z
is control minus treatment, so it is negative when treatment converts better in that segment. Keep that sign.
The test is two-sided, and the comparison with 1.96 is strict.
There is no user id, so every row counts as one user. Do not deduplicate identical rows.