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.

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

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 ncn_c and ntn_t users and xcx_c and xtx_t conversions in control and treatment:

p^c=xcnc,p^t=xtnt,p^=xc+xtnc+nt\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=p^c−p^tp^ (1−p^)(1nc+1nt)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 p^\hat p and the group sizes all come from that segment's rows only.

Input Table(s)

Table trials:

ColumnTypeMeaning
segmentVARCHAR(20)User segment, either 'mobile' or 'desktop'
group_nameVARCHAR(20)Experiment arm, either 'control' or 'treatment'
convertedSMALLINT1 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:

ColumnTypeRule
segmentVARCHARThe segment
z_statisticNUMERICThat segment's zz (control minus treatment), rounded to 4 decimal places, or NULL when it is undefined
significantINTEGER1 when the absolute value of zz 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 zz.

Example

Input trials:

segmentgroup_nameconverted
desktopcontrol0
desktopcontrol0
desktopcontrol0
mobiletreatment0
mobilecontrol0
mobiletreatment1
mobiletreatment1
desktopcontrol0
mobiletreatment1
desktopcontrol0
desktoptreatment0
desktoptreatment0
desktoptreatment0
desktopcontrol1
mobilecontrol0
mobiletreatment0
desktopcontrol1
desktoptreatment0
mobiletreatment0
desktopcontrol1
mobilecontrol0
mobiletreatment0
mobilecontrol0
mobiletreatment1
desktoptreatment0

Expected output:

segmentz_statisticsignificant
desktop1.56120
mobile-1.73210

Desktop has 8 control users with 3 conversions and 5 treatment users with none, so its pooled rate is 3/13 and z=38/3/52≈1.5612z = \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/1/12=−3≈−1.7321z = -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.
  • zz 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.