Compute conversion rate and Wald standard error for each A/B test variant in SQL

Quick Overview

Write a SQL query that returns, for each A/B test variant, the conversion rate and its Wald standard error from a per-user trials table. It tests grouped aggregation, decimal division, the binomial variance formula and the edge case of a rate of exactly 0 or 1.

Compute conversion rate and Wald standard error for each A/B test variant in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

The `trials` table has one row per user with the `variant` the user saw and whether the user `converted` (1 or 0). For each variant, compute the conversion rate `conv_rate = conversions / users` and its Wald standard error `std_error = sqrt(conv_rate * (1 - conv_rate) / users)`. Return one row per variant. ### Input Table(s) `trials` | Column | Type | Meaning | |---|---|---| | variant | VARCHAR(10) | Name of the variant the user saw | | converted | SMALLINT | 1 if the user converted, 0 if not | Each row is one user. The table has no user id column, so there is no declared key; every row counts as a distinct user. The number of variants is not fixed. ### Output Contract One row per distinct `variant`, with these columns in this order: | Column | Definition | Rounding | |---|---|---| | variant | The variant name | none | | conv_rate | conversions / users for the variant | 4 decimals | | std_error | sqrt(conv_rate * (1 - conv_rate) / users) | 4 decimals | Compute `std_error` from the unrounded rate and round only the final values. Sort by `variant` ascending. ### Example Input `trials`: | variant | converted | |---|---| | B | 1 | | A | 1 | | B | 1 | | A | 1 | | C | 1 | | A | 1 | | A | 1 | | A | 1 | | C | 1 | | A | 0 | | B | 0 | | C | 1 | | C | 1 | | B | 0 | | B | 0 | | A | 0 | | A | 1 | | B | 0 | | B | 0 | | C | 1 | | C | 1 | | C | 1 | Expected result: | variant | conv_rate | std_error | |---|---|---| | A | 0.7500 | 0.1531 | | B | 0.2857 | 0.1707 | | C | 1.0000 | 0.0000 | Variant A has 8 users and 6 conversions, so its rate is 0.75 and its standard error is sqrt(0.75 * 0.25 / 8) = 0.1531. Variant B has 2 conversions out of 7 users (0.2857, standard error 0.1707), and every one of C's 7 users converted, so its rate is 1 and the Wald formula gives 0. ### Constraints and Clarifications - `variant` is never NULL. - `converted` is never NULL and is always 0 or 1. - Every row is a different user, so there is nothing to deduplicate. - Use the population form of the variance, p(1 - p), divided by the group's user count. Do not apply an n - 1 correction. - A variant whose rate is exactly 0 or 1 gets a standard error of 0. ```hint Watch the arithmetic types Both counts are integers. Check what type the division produces before you take a square root of it. ```

Overview: Write a SQL query that returns, for each A/B test variant, the conversion rate and its Wald standard error from a per-user trials table. It tests grouped aggregation, decimal division, the binomial variance formula and the edge case of a rate of exactly 0 or 1.

General logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

The trials table has one row per user with the variant the user saw and whether the user converted (1 or 0). For each variant, compute the conversion rate conv_rate = conversions / users and its Wald standard error std_error = sqrt(conv_rate * (1 - conv_rate) / users). Return one row per variant.

Input Table(s)

trials

ColumnTypeMeaning
variantVARCHAR(10)Name of the variant the user saw
convertedSMALLINT1 if the user converted, 0 if not

Each row is one user. The table has no user id column, so there is no declared key; every row counts as a distinct user. The number of variants is not fixed.

Output Contract

One row per distinct variant, with these columns in this order:

ColumnDefinitionRounding
variantThe variant namenone
conv_rateconversions / users for the variant4 decimals
std_errorsqrt(conv_rate * (1 - conv_rate) / users)4 decimals

Compute std_error from the unrounded rate and round only the final values. Sort by variant ascending.

Example

Input trials:

variantconverted
B1
A1
B1
A1
C1
A1
A1
A1
C1
A0
B0
C1
C1
B0
B0
A0
A1
B0
B0
C1
C1
C1

Expected result:

variantconv_ratestd_error
A0.75000.1531
B0.28570.1707
C1.00000.0000

Variant A has 8 users and 6 conversions, so its rate is 0.75 and its standard error is sqrt(0.75 * 0.25 / 8) = 0.1531. Variant B has 2 conversions out of 7 users (0.2857, standard error 0.1707), and every one of C's 7 users converted, so its rate is 1 and the Wald formula gives 0.

Constraints and Clarifications

  • variant is never NULL.
  • converted is never NULL and is always 0 or 1.
  • Every row is a different user, so there is nothing to deduplicate.
  • Use the population form of the variance, p(1 - p), divided by the group's user count. Do not apply an n - 1 correction.
  • A variant whose rate is exactly 0 or 1 gets a standard error of 0.