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.
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=r1r2k1k2n(ad−bc)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
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 χ2=686/2401≈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.