Test A/B significance in SQL by joining a chi-square critical-value lookup table

Quick Overview

Compute the Pearson chi-square statistic of a two-variant conversion test in SQL, derive its degrees of freedom, and join a lookup table of critical values to decide significance at alpha 0.05. Tests conditional aggregation, joining on a computed key and exact numeric comparison.

Test A/B significance in SQL by joining a chi-square critical-value lookup table

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

`ab_events` has one row per visitor in a two-variant conversion test, with the variant the visitor saw and whether they converted. `critical_values` is a seeded lookup of chi-square critical values by degrees of freedom and significance level. Compute the Pearson chi-square statistic of the 2x2 variant-by-outcome table and its degrees of freedom, then compare the statistic with the critical value for those degrees of freedom at `alpha` = 0.05. The threshold must come from the lookup table. Use the 2x2 shortcut formula $$\chi^2 = \frac{n\,(ad - bc)^2}{r_1\, r_2\, k_1\, k_2}$$ with this cell layout: | | 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 | ```hint Start from the table's shape The degrees of freedom of a contingency table depend only on how many rows and columns it has. Settle that number first, because the join key depends on it. ``` ### 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. `critical_values` | Column | Type | Meaning | |---|---|---| | df | INT | Degrees of freedom | | alpha | DECIMAL(4,3) | Significance level | | critical_value | DECIMAL(8,4) | Upper-tail critical value of the chi-square distribution for this `df` and `alpha` | Key: (`df`, `alpha`) is unique. ### Output Contract Exactly one row with these columns, in this order: | Column | Type | Meaning | |---|---|---| | chi_square | NUMERIC | The Pearson statistic, rounded to 4 decimal places | | df | INTEGER | Degrees of freedom of the 2x2 table | | critical_value | DECIMAL(8,4) | The lookup value for this `df` at `alpha` = 0.05, unchanged | | significant | INTEGER | 1 when the unrounded statistic is greater than `critical_value`, else 0 | The result always has one row, so no ordering is needed. ### Example Input `ab_events` (14 rows): | variant | outcome | |---|---| | control | converted | | control | not_converted | | treatment | not_converted | | treatment | converted | | control | not_converted | | control | not_converted | | treatment | not_converted | | control | not_converted | | treatment | converted | | control | converted | | control | converted | | treatment | not_converted | | treatment | converted | | treatment | converted | Input `critical_values`: | df | alpha | critical_value | |---|---|---| | 1 | 0.05 | 3.841 | | 2 | 0.05 | 5.991 | | 3 | 0.05 | 7.815 | | 4 | 0.05 | 9.488 | Expected output: | chi_square | df | critical_value | significant | |---|---|---|---| | 0.2857 | 1 | 3.841 | 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 = 686 / 2401 \approx 0.2857$. A 2x2 table has one degree of freedom, the lookup gives 3.841 for it at 0.05, and the statistic is below that, so `significant` is 0. ### Constraints and Clarifications - `variant` is always `'control'` or `'treatment'`, and `outcome` is always `'converted'` or `'not_converted'`. No column in either table contains NULLs. - Both variants and both outcomes appear at least once, so every row and column total is positive and the statistic is defined. - `critical_values` may hold rows for other significance levels. It always holds exactly one row for the degrees of freedom you need at `alpha` = 0.05. - Use the formula exactly as given, with no continuity correction. - `significant` compares the unrounded statistic with `critical_value` using a strict "greater than". A statistic equal to the critical value 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 the Pearson chi-square statistic of a two-variant conversion test in SQL, derive its degrees of freedom, and join a lookup table of critical values to decide significance at alpha 0.05. Tests conditional aggregation, joining on a computed key and exact numeric comparison.

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

ab_events has one row per visitor in a two-variant conversion test, with the variant the visitor saw and whether they converted. critical_values is a seeded lookup of chi-square critical values by degrees of freedom and significance level. Compute the Pearson chi-square statistic of the 2x2 variant-by-outcome table and its degrees of freedom, then compare the statistic with the critical value for those degrees of freedom at alpha = 0.05. The threshold must come from the lookup table.

Use the 2x2 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}

with this cell layout:

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

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.

critical_values

ColumnTypeMeaning
dfINTDegrees of freedom
alphaDECIMAL(4,3)Significance level
critical_valueDECIMAL(8,4)Upper-tail critical value of the chi-square distribution for this df and alpha

Key: (df, alpha) is unique.

Output Contract

Exactly one row with these columns, in this order:

ColumnTypeMeaning
chi_squareNUMERICThe Pearson statistic, rounded to 4 decimal places
dfINTEGERDegrees of freedom of the 2x2 table
critical_valueDECIMAL(8,4)The lookup value for this df at alpha = 0.05, unchanged
significantINTEGER1 when the unrounded statistic is greater than critical_value, else 0

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

Example

Input ab_events (14 rows):

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

Input critical_values:

dfalphacritical_value
10.053.841
20.055.991
30.057.815
40.059.488

Expected output:

chi_squaredfcritical_valuesignificant
0.285713.8410

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=686/2401≈0.2857\chi^2 = 686 / 2401 \approx 0.2857. A 2x2 table has one degree of freedom, the lookup gives 3.841 for it at 0.05, and the statistic is below that, so significant is 0.

Constraints and Clarifications

  • variant is always 'control' or 'treatment' , and outcome is always 'converted' or 'not_converted' . No column in either table contains NULLs.
  • Both variants and both outcomes appear at least once, so every row and column total is positive and the statistic is defined.
  • critical_values may hold rows for other significance levels. It always holds exactly one row for the degrees of freedom you need at alpha = 0.05.
  • Use the formula exactly as given, with no continuity correction.
  • significant compares the unrounded statistic with critical_value using a strict "greater than". A statistic equal to the critical value gives 0.
  • The query should stay correct at production sizes (hundreds of thousands of visitors per variant), not only on the small example.