Confidence Interval for a Difference in Conversion Rates in SQL

Quick Overview

Write a PostgreSQL query that computes the 95% confidence interval for the difference in conversion rates between treatment and control from a per-user A/B test table. It tests conditional aggregation, the unpooled standard error, and keeping values unrounded until the final output.

Confidence Interval for a Difference in Conversion Rates 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. Build the 95% confidence interval for the difference in conversion rates (treatment minus control) using the unpooled standard error. Return one row with `rate_diff`, `ci_lower` and `ci_upper`, each rounded to 4 decimals. With $n_c$ users and $x_c$ conversions in control and $n_t$ users and $x_t$ conversions in treatment: $$ \hat p_c = \frac{x_c}{n_c}, \quad \hat p_t = \frac{x_t}{n_t}, \quad \text{rate\_diff} = \hat p_t - \hat p_c $$ $$ SE = \sqrt{\frac{\hat p_c(1 - \hat p_c)}{n_c} + \frac{\hat p_t(1 - \hat p_t)}{n_t}}, \qquad \text{CI} = \text{rate\_diff} \pm 1.96 \cdot SE $$ ```hint Watch the rounding Get the per-group counts into one row first, and keep every intermediate value unrounded until the final SELECT. ``` ### 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 | |---|---|---| | `rate_diff` | NUMERIC | $\hat p_t - \hat p_c$, rounded to 4 decimal places | | `ci_lower` | NUMERIC | $\text{rate\_diff} - 1.96 \cdot SE$, rounded to 4 decimal places | | `ci_upper` | NUMERIC | $\text{rate\_diff} + 1.96 \cdot SE$, rounded to 4 decimal places | The result is a single row, so no ORDER BY is needed. Each column is rounded from the unrounded difference and standard error. Do not build the bounds from an already rounded `rate_diff`. ### Example Input `trials`: | group_name | converted | |---|---| | treatment | 1 | | control | 1 | | treatment | 1 | | treatment | 1 | | control | 1 | | treatment | 0 | | control | 1 | | treatment | 0 | | control | 0 | | control | 0 | | control | 0 | | treatment | 0 | | treatment | 0 | | treatment | 1 | | control | 0 | | control | 0 | | treatment | 1 | | control | 0 | | control | 1 | Expected output: | rate_diff | ci_lower | ci_upper | |---|---|---| | 0.1556 | -0.2890 | 0.6001 | Control has 10 users with 4 conversions (rate 0.4) and treatment has 9 users with 5 conversions (rate 5/9, about 0.5556), so the difference is about 0.15556. The standard error is $\sqrt{0.024 + 20/729} \approx 0.22679$, and ${1.96 \cdot SE \approx 0.44451}$, which puts the bounds at about -0.28896 and 0.60007. ### 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. A group where every user has the same outcome contributes a variance term of 0, and the interval is still defined. - `rate_diff` is treatment minus control. Keep that sign. - There is no user id, so every row counts as one user. Do not deduplicate identical rows. - Rounding follows PostgreSQL's `ROUND` on NUMERIC, which rounds halves away from zero.

Overview: Write a PostgreSQL query that computes the 95% confidence interval for the difference in conversion rates between treatment and control from a per-user A/B test table. It tests conditional aggregation, the unpooled standard error, and keeping values unrounded until the final output.

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. Build the 95% confidence interval for the difference in conversion rates (treatment minus control) using the unpooled standard error. Return one row with rate_diff, ci_lower and ci_upper, each rounded to 4 decimals.

With ncn_c users and xcx_c conversions in control and ntn_t users and xtx_t conversions in treatment:

p^c=xcnc,p^t=xtnt,rate_diff=p^t−p^c\hat p_c = \frac{x_c}{n_c}, \quad \hat p_t = \frac{x_t}{n_t}, \quad \text{rate\_diff} = \hat p_t - \hat p_c SE=p^c(1−p^c)nc+p^t(1−p^t)nt,CI=rate_diff±1.96⋅SESE = \sqrt{\frac{\hat p_c(1 - \hat p_c)}{n_c} + \frac{\hat p_t(1 - \hat p_t)}{n_t}}, \qquad \text{CI} = \text{rate\_diff} \pm 1.96 \cdot SE

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
rate_diffNUMERICp^t−p^c\hat p_t - \hat p_c, rounded to 4 decimal places
ci_lowerNUMERICrate_diff−1.96⋅SE\text{rate\_diff} - 1.96 \cdot SE, rounded to 4 decimal places
ci_upperNUMERICrate_diff+1.96⋅SE\text{rate\_diff} + 1.96 \cdot SE, rounded to 4 decimal places

The result is a single row, so no ORDER BY is needed. Each column is rounded from the unrounded difference and standard error. Do not build the bounds from an already rounded rate_diff.

Example

Input trials:

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

Expected output:

rate_diffci_lowerci_upper
0.1556-0.28900.6001

Control has 10 users with 4 conversions (rate 0.4) and treatment has 9 users with 5 conversions (rate 5/9, about 0.5556), so the difference is about 0.15556. The standard error is 0.024+20/729≈0.22679\sqrt{0.024 + 20/729} \approx 0.22679, and 1.96⋅SE≈0.44451{1.96 \cdot SE \approx 0.44451}, which puts the bounds at about -0.28896 and 0.60007.

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. A group where every user has the same outcome contributes a variance term of 0, and the interval is still defined.
  • rate_diff is treatment minus control. Keep that sign.
  • There is no user id, so every row counts as one user. Do not deduplicate identical rows.
  • Rounding follows PostgreSQL's ROUND on NUMERIC, which rounds halves away from zero.