A/B Test Significance Decision with a Pooled Two-Proportion Z-Test in SQL

Quick Overview

Write a PostgreSQL query that turns a per-user A/B test table into a pooled two-proportion z-statistic and a significance decision at the 95% level. It tests conditional aggregation, avoiding integer division, and applying a two-sided test with the right sign convention.

A/B Test Significance Decision with a Pooled Two-Proportion Z-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 is in either the `control` or the `treatment` group, and `converted` is 1 if the user converted and 0 otherwise. Compute the pooled two-proportion z-statistic for the difference between the control and treatment conversion rates, then decide whether the difference is significant at the 95% level with a two-sided test. Return a single row with `z_statistic`, `critical_value`, `significant` and `decision`. 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)}} $$ ```hint Get the counts first Reduce the table to one row that holds the user count and the conversion count of each group before you write the formula, and check that no step divides one integer by another. ``` ### Input Table(s) Table `trials`: | Column | Type | Meaning | |---|---|---| | `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 Exactly one row with these columns, in this order: | Column | Type | Rule | |---|---|---| | `z_statistic` | NUMERIC | $z$ as defined above (control minus treatment), rounded to 4 decimal places | | `critical_value` | NUMERIC | Always 1.96 | | `significant` | INTEGER | 1 when the absolute value of $z$ is greater than 1.96, else 0 | | `decision` | TEXT | `'reject H0'` when `significant` is 1, else `'fail to reject H0'` | The result is a single row, so no ORDER BY is needed. Rounding applies only to the `z_statistic` column. The significance decision uses the unrounded $z$. ### Example Input `trials`: | group_name | converted | |---|---| | control | 0 | | treatment | 0 | | treatment | 1 | | control | 0 | | treatment | 1 | | control | 0 | | control | 0 | | treatment | 1 | | treatment | 0 | | control | 0 | | treatment | 1 | | treatment | 0 | | treatment | 1 | | control | 0 | | control | 0 | | control | 1 | | control | 0 | | treatment | 1 | Expected output: | z_statistic | critical_value | significant | decision | |---|---|---|---| | -2.4175 | 1.96 | 1 | reject H0 | Control has 9 users with 1 conversion and treatment has 9 users with 6 conversions, so the pooled rate is 7/18. The numerator is $\tfrac{1}{9} - \tfrac{6}{9} = -\tfrac{5}{9}$ and the standard error is $\sqrt{\tfrac{7}{18} \cdot \tfrac{11}{18} \cdot \tfrac{2}{9}} \approx 0.2298$, which gives $z = -30/\sqrt{154} \approx -2.4175$. Its absolute value is above 1.96, so the difference is significant. ### Constraints and Clarifications - `group_name` only takes the values `'control'` and `'treatment'`. - `converted` is always 0 or 1 and never NULL. - Both groups have at least one user, and the table has at least one converted and one non-converted user, so the standard error is positive. - $z$ is control minus treatment, so it is negative when treatment converts better. Keep that sign. - The test is two-sided: a large negative $z$ counts the same as a large positive one. The comparison with 1.96 is strict, so an absolute value of exactly 1.96 is not significant. - There is no user id, so every row counts as one user. Do not deduplicate identical rows.

Overview: Write a PostgreSQL query that turns a per-user A/B test table into a pooled two-proportion z-statistic and a significance decision at the 95% level. It tests conditional aggregation, avoiding integer division, and applying a two-sided test with the right sign convention.

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 is in either the control or the treatment group, and converted is 1 if the user converted and 0 otherwise. Compute the pooled two-proportion z-statistic for the difference between the control and treatment conversion rates, then decide whether the difference is significant at the 95% level with a two-sided test. Return a single row with z_statistic, critical_value, significant and decision.

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)}}

Input Table(s)

Table trials:

ColumnTypeMeaning
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

Exactly one row with these columns, in this order:

ColumnTypeRule
z_statisticNUMERICzz as defined above (control minus treatment), rounded to 4 decimal places
critical_valueNUMERICAlways 1.96
significantINTEGER1 when the absolute value of zz is greater than 1.96, else 0
decisionTEXT'reject H0' when significant is 1, else 'fail to reject H0'

The result is a single row, so no ORDER BY is needed. Rounding applies only to the z_statistic column. The significance decision uses the unrounded zz.

Example

Input trials:

group_nameconverted
control0
treatment0
treatment1
control0
treatment1
control0
control0
treatment1
treatment0
control0
treatment1
treatment0
treatment1
control0
control0
control1
control0
treatment1

Expected output:

z_statisticcritical_valuesignificantdecision
-2.41751.961reject H0

Control has 9 users with 1 conversion and treatment has 9 users with 6 conversions, so the pooled rate is 7/18. The numerator is 19−69=−59\tfrac{1}{9} - \tfrac{6}{9} = -\tfrac{5}{9} and the standard error is 718⋅1118⋅29≈0.2298\sqrt{\tfrac{7}{18} \cdot \tfrac{11}{18} \cdot \tfrac{2}{9}} \approx 0.2298, which gives z=−30/154≈−2.4175z = -30/\sqrt{154} \approx -2.4175. Its absolute value is above 1.96, so the difference is significant.

Constraints and Clarifications

  • group_name only takes the values 'control' and 'treatment' .
  • converted is always 0 or 1 and never NULL.
  • Both groups have at least one user, and the table has at least one converted and one non-converted user, so the standard error is positive.
  • zz is control minus treatment, so it is negative when treatment converts better. Keep that sign.
  • The test is two-sided: a large negative zz counts the same as a large positive one. The comparison with 1.96 is strict, so an absolute value of exactly 1.96 is not significant.
  • There is no user id, so every row counts as one user. Do not deduplicate identical rows.