Compute a chi-square statistic and significance flag for an A/B test in SQL

Quick Overview

Compute Pearson's chi-square statistic for a two-variant conversion test in SQL from one row per visitor, then flag whether it clears the critical value for one degree of freedom. Tests conditional aggregation into a 2x2 table, exact numeric arithmetic and a careful threshold comparison.

Compute a chi-square statistic and significance flag for an A/B test in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Table `ab_events` has one row per visitor in a two-variant conversion test. Each row records which variant the visitor saw and whether they converted. Collapse the rows into the 2x2 variant-by-outcome table and compute Pearson's chi-square statistic with the shortcut formula $$\chi^2 = \frac{n\,(ad - bc)^2}{r_1\, r_2\, k_1\, k_2}$$ where $a, b, c, d$ are the four cell counts, $r_1, r_2$ are the row totals, $k_1, k_2$ are the column totals and $n$ is the number of visitors: | | converted | not_converted | row total | |---|---|---|---| | control | a | b | r1 = a + b | | treatment | c | d | r2 = c + d | | column total | k1 = a + c | k2 = b + d | n | Return one row with `chi_square` rounded to 4 decimals and `significant`, which is 1 if the statistic exceeds 3.841 (the critical value for one degree of freedom at the 5% level) and 0 otherwise. ```hint Watch the arithmetic types The counts come out of aggregation as integers. Think about what integer division, and a product of four large totals, do to the result before you divide. ``` ### Input Table(s) `ab_events` | Column | Type | Meaning | |---|---|---| | variant | VARCHAR(20) | The arm the visitor saw: `'control'` or `'treatment'` | | outcome | VARCHAR(20) | `'converted'` or `'not_converted'` | There is no primary key. Each row is one visitor, so two identical rows are two different visitors. ### Output Contract Exactly one row with two columns: | Column | Type | Meaning | |---|---|---| | chi_square | NUMERIC | The Pearson statistic, rounded to 4 decimal places | | significant | INTEGER | 1 when the unrounded statistic is greater than 3.841, else 0 | The result always has one row, so no ordering is needed. ### Example Input `ab_events` (14 rows): | variant | outcome | |---|---| | treatment | converted | | treatment | not_converted | | treatment | not_converted | | control | not_converted | | treatment | not_converted | | control | not_converted | | control | not_converted | | control | converted | | control | converted | | control | converted | | treatment | converted | | treatment | converted | | control | not_converted | | treatment | converted | Expected output: | chi_square | significant | |---|---| | 0.2857 | 0 | Control has 3 converted and 4 not converted visitors, and treatment has 4 and 3. Every row and column total is 7 and $n = 14$, so $$\chi^2 = \frac{14 \cdot (3 \cdot 3 - 4 \cdot 4)^2}{7^4} = \frac{686}{2401} \approx 0.2857,$$ which is far below 3.841. ### Constraints and Clarifications - `variant` is always `'control'` or `'treatment'`, and `outcome` is always `'converted'` or `'not_converted'`. Neither column contains NULLs. - Both variants and both outcomes appear at least once, so every row and column total is positive and the statistic is defined. - Use the formula exactly as given, with no continuity correction. - `significant` compares the unrounded statistic with a strict "greater than". A statistic equal to 3.841 gives 0. - The query should stay correct at production sizes (hundreds of thousands of visitors per variant), not only on the small example.

Overview: Compute Pearson's chi-square statistic for a two-variant conversion test in SQL from one row per visitor, then flag whether it clears the critical value for one degree of freedom. Tests conditional aggregation into a 2x2 table, exact numeric arithmetic and a careful threshold comparison.

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

Table ab_events has one row per visitor in a two-variant conversion test. Each row records which variant the visitor saw and whether they converted. Collapse the rows into the 2x2 variant-by-outcome table and compute Pearson's chi-square statistic with the shortcut formula

χ2=n (ad−bc)2r1 r2 k1 k2\chi^2 = \frac{n\,(ad - bc)^2}{r_1\, r_2\, k_1\, k_2}

where a,b,c,da, b, c, d are the four cell counts, r1,r2r_1, r_2 are the row totals, k1,k2k_1, k_2 are the column totals and nn is the number of visitors:

convertednot_convertedrow total
controlabr1 = a + b
treatmentcdr2 = c + d
column totalk1 = a + ck2 = b + dn

Return one row with chi_square rounded to 4 decimals and significant, which is 1 if the statistic exceeds 3.841 (the critical value for one degree of freedom at the 5% level) and 0 otherwise.

Input Table(s)

ab_events

ColumnTypeMeaning
variantVARCHAR(20)The arm the visitor saw: 'control' or 'treatment'
outcomeVARCHAR(20)'converted' or 'not_converted'

There is no primary key. Each row is one visitor, so two identical rows are two different visitors.

Output Contract

Exactly one row with two columns:

ColumnTypeMeaning
chi_squareNUMERICThe Pearson statistic, rounded to 4 decimal places
significantINTEGER1 when the unrounded statistic is greater than 3.841, else 0

The result always has one row, so no ordering is needed.

Example

Input ab_events (14 rows):

variantoutcome
treatmentconverted
treatmentnot_converted
treatmentnot_converted
controlnot_converted
treatmentnot_converted
controlnot_converted
controlnot_converted
controlconverted
controlconverted
controlconverted
treatmentconverted
treatmentconverted
controlnot_converted
treatmentconverted

Expected output:

chi_squaresignificant
0.28570

Control has 3 converted and 4 not converted visitors, and treatment has 4 and 3. Every row and column total is 7 and n=14n = 14, so

χ2=14⋅(3⋅3−4⋅4)274=6862401≈0.2857,\chi^2 = \frac{14 \cdot (3 \cdot 3 - 4 \cdot 4)^2}{7^4} = \frac{686}{2401} \approx 0.2857,

which is far below 3.841.

Constraints and Clarifications

  • variant is always 'control' or 'treatment' , and outcome is always 'converted' or 'not_converted' . Neither column contains NULLs.
  • Both variants and both outcomes appear at least once, so every row and column total is positive and the statistic is defined.
  • Use the formula exactly as given, with no continuity correction.
  • significant compares the unrounded statistic with a strict "greater than". A statistic equal to 3.841 gives 0.
  • The query should stay correct at production sizes (hundreds of thousands of visitors per variant), not only on the small example.