Chi-Square Homogeneity Test Across Three or More Variants in SQL

Quick Overview

Write a PostgreSQL query that computes the chi-square test of homogeneity for three or more experiment variants with a converted or not converted outcome. It tests building a complete contingency table with marginals, expected counts and empty cells, and deriving the degrees of freedom.

Chi-Square Homogeneity Test Across Three or More Variants in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

The `trials` table has one row per user in an experiment with three or more variants (for example `'A'`, `'B'`, `'C'`). Each user's `outcome` is either `'converted'` or `'not_converted'`. Use a chi-square test of homogeneity to check whether conversion is distributed the same way in every variant. Build the variant-by-outcome table. For the cell in variant row $i$ and outcome column $j$, the expected count is the row total times the column total divided by the grand total: $$E_{ij} = \frac{R_i \cdot C_j}{N}$$ Return one row with `chi_square`, the sum over all cells of $(O_{ij} - E_{ij})^2 / E_{ij}$ rounded to 4 decimals, and `df` $= (g - 1)(k - 1)$, where $g$ is the number of variants and $k$ is the number of outcomes. ```hint Watch the division In PostgreSQL, `COUNT(*)` returns `bigint`, and dividing one `bigint` by another truncates. Check what type your expected counts end up with. ``` ### Input Table(s) `trials` | Column | Type | Meaning | |---|---|---| | variant | VARCHAR(20) | Experiment group the user was assigned to | | outcome | VARCHAR(20) | `'converted'` or `'not_converted'` | The table has no key column. Each row is one user, so many rows share the same (`variant`, `outcome`) values. ### Output Contract | Column | Type | Meaning | |---|---|---| | chi_square | NUMERIC | Chi-square homogeneity statistic, rounded to 4 decimal places | | df | INTEGER | Degrees of freedom, $(g - 1)(k - 1)$ | The result is exactly one row, so no ordering is needed. ### Example Input `trials`: | variant | outcome | |---|---| | A | not_converted | | B | not_converted | | C | converted | | A | converted | | C | not_converted | | C | not_converted | | B | converted | | C | converted | | B | not_converted | | C | not_converted | | A | not_converted | | C | not_converted | | B | not_converted | | C | converted | | A | not_converted | | B | converted | | A | converted | | A | converted | | B | not_converted | | A | not_converted | | A | converted | | B | not_converted | Output: | chi_square | df | |---|---| | 0.7253 | 2 | Variant A has 8 users (4 converted), B has 7 (2 converted) and C has 7 (3 converted), for 22 users and 9 conversions in total. With 3 variants and 2 outcomes, df is 2. ### Constraints and Clarifications - Neither column is NULL. - There are at least three variants, and their labels are not known in advance, so do not hard-code them. - Both outcome values appear somewhere in the table, and every variant has at least one user, so every expected count is positive. - A variant can have zero users with one of the outcomes. That cell is still part of the table, with an observed count of 0. - Each row is a different user. Count rows as they are; there is nothing to deduplicate. - Round only the final statistic, not the individual cell terms.

Overview: Write a PostgreSQL query that computes the chi-square test of homogeneity for three or more experiment variants with a converted or not converted outcome. It tests building a complete contingency table with marginals, expected counts and empty cells, and deriving the degrees of freedom.

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

The trials table has one row per user in an experiment with three or more variants (for example 'A', 'B', 'C'). Each user's outcome is either 'converted' or 'not_converted'. Use a chi-square test of homogeneity to check whether conversion is distributed the same way in every variant.

Build the variant-by-outcome table. For the cell in variant row ii and outcome column jj, the expected count is the row total times the column total divided by the grand total:

Eij=Ri⋅CjNE_{ij} = \frac{R_i \cdot C_j}{N}

Return one row with chi_square, the sum over all cells of (Oij−Eij)2/Eij(O_{ij} - E_{ij})^2 / E_{ij} rounded to 4 decimals, and df =(g−1)(k−1)= (g - 1)(k - 1), where gg is the number of variants and kk is the number of outcomes.

Input Table(s)

trials

ColumnTypeMeaning
variantVARCHAR(20)Experiment group the user was assigned to
outcomeVARCHAR(20)'converted' or 'not_converted'

The table has no key column. Each row is one user, so many rows share the same (variant, outcome) values.

Output Contract

ColumnTypeMeaning
chi_squareNUMERICChi-square homogeneity statistic, rounded to 4 decimal places
dfINTEGERDegrees of freedom, (g−1)(k−1)(g - 1)(k - 1)

The result is exactly one row, so no ordering is needed.

Example

Input trials:

variantoutcome
Anot_converted
Bnot_converted
Cconverted
Aconverted
Cnot_converted
Cnot_converted
Bconverted
Cconverted
Bnot_converted
Cnot_converted
Anot_converted
Cnot_converted
Bnot_converted
Cconverted
Anot_converted
Bconverted
Aconverted
Aconverted
Bnot_converted
Anot_converted
Aconverted
Bnot_converted

Output:

chi_squaredf
0.72532

Variant A has 8 users (4 converted), B has 7 (2 converted) and C has 7 (3 converted), for 22 users and 9 conversions in total. With 3 variants and 2 outcomes, df is 2.

Constraints and Clarifications

  • Neither column is NULL.
  • There are at least three variants, and their labels are not known in advance, so do not hard-code them.
  • Both outcome values appear somewhere in the table, and every variant has at least one user, so every expected count is positive.
  • A variant can have zero users with one of the outcomes. That cell is still part of the table, with an observed count of 0.
  • Each row is a different user. Count rows as they are; there is nothing to deduplicate.
  • Round only the final statistic, not the individual cell terms.