Chi-Square Test of Independence Statistic for a 2x2 Table

Quick Overview

Write a SQL query that builds a 2x2 contingency table from per-subject category labels and returns the Pearson chi-square statistic and its degrees of freedom. It tests conditional aggregation, expected counts from row and column totals, keeping empty cells in the sum, and avoiding integer division.

Chi-Square Test of Independence Statistic for a 2x2 Table

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

The `observations` table has one row per subject with `row_cat` (`'A'` or `'B'`) and `col_cat` (`'X'` or `'Y'`). Build the 2x2 contingency table and compute each cell's expected count as $$ E = \frac{\text{row total} \times \text{column total}}{\text{grand total}} $$ Return one row with the Pearson statistic $$ \chi^2 = \sum_{\text{cells}} \frac{(O - E)^2}{E} $$ rounded to 4 decimals as `chi_square`, and `(rows - 1) * (cols - 1)` as `df`, where $O$ is the observed count in a cell. ```hint Empty cells A cell with no subjects still contributes to the statistic. Make sure your counting step cannot drop it. ``` ### Input Table(s) `observations` | Column | Type | Meaning | |---|---|---| | row_cat | VARCHAR(5) | Row attribute of the subject, `'A'` or `'B'` | | col_cat | VARCHAR(5) | Column attribute of the subject, `'X'` or `'Y'` | There is no key column. Each row is one subject, and the same (`row_cat`, `col_cat`) pair can appear many times. ### Output Contract - Exactly one row with the columns `chi_square` and `df`, in that order. - `chi_square` is rounded to 4 decimals. Round only the final sum. - `df` is an integer, the number of row levels minus 1 times the number of column levels minus 1. - The result has a single row, so no ordering is needed. ### Example Input `observations`: | row_cat | col_cat | |---|---| | B | Y | | B | Y | | B | X | | A | Y | | B | Y | | A | Y | | A | X | | A | Y | | A | Y | | A | Y | | A | X | | B | X | | B | Y | | A | X | | B | X | Expected output: | chi_square | df | |---|---| | 0.0446 | 1 | The cell counts are A,X = 3, A,Y = 5, B,X = 3 and B,Y = 4, so the row totals are 8 and 7, the column totals are 6 and 9, and the grand total is 15. The expected counts are 3.2, 4.8, 2.8 and 4.2. Each cell is off by 0.2, and the four contributions 0.04/3.2, 0.04/4.8, 0.04/2.8 and 0.04/4.2 sum to about 0.044643. ### Constraints and Clarifications - `row_cat` is always `'A'` or `'B'`, `col_cat` is always `'X'` or `'Y'`, and neither is NULL. - Both row levels and both column levels appear at least once, so every expected count is positive. A single cell may still have zero subjects. - Repeated rows are separate subjects and each one counts. - Report the uncorrected Pearson statistic, with no continuity correction.

Overview: Write a SQL query that builds a 2x2 contingency table from per-subject category labels and returns the Pearson chi-square statistic and its degrees of freedom. It tests conditional aggregation, expected counts from row and column totals, keeping empty cells in the sum, and avoiding integer division.

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

The observations table has one row per subject with row_cat ('A' or 'B') and col_cat ('X' or 'Y'). Build the 2x2 contingency table and compute each cell's expected count as

E=row total×column totalgrand totalE = \frac{\text{row total} \times \text{column total}}{\text{grand total}}

Return one row with the Pearson statistic

χ2=∑cells(O−E)2E\chi^2 = \sum_{\text{cells}} \frac{(O - E)^2}{E}

rounded to 4 decimals as chi_square, and (rows - 1) * (cols - 1) as df, where OO is the observed count in a cell.

Input Table(s)

observations

ColumnTypeMeaning
row_catVARCHAR(5)Row attribute of the subject, 'A' or 'B'
col_catVARCHAR(5)Column attribute of the subject, 'X' or 'Y'

There is no key column. Each row is one subject, and the same (row_cat, col_cat) pair can appear many times.

Output Contract

  • Exactly one row with the columns chi_square and df , in that order.
  • chi_square is rounded to 4 decimals. Round only the final sum.
  • df is an integer, the number of row levels minus 1 times the number of column levels minus 1.
  • The result has a single row, so no ordering is needed.

Example

Input observations:

row_catcol_cat
BY
BY
BX
AY
BY
AY
AX
AY
AY
AY
AX
BX
BY
AX
BX

Expected output:

chi_squaredf
0.04461

The cell counts are A,X = 3, A,Y = 5, B,X = 3 and B,Y = 4, so the row totals are 8 and 7, the column totals are 6 and 9, and the grand total is 15. The expected counts are 3.2, 4.8, 2.8 and 4.2. Each cell is off by 0.2, and the four contributions 0.04/3.2, 0.04/4.8, 0.04/2.8 and 0.04/4.2 sum to about 0.044643.

Constraints and Clarifications

  • row_cat is always 'A' or 'B' , col_cat is always 'X' or 'Y' , and neither is NULL.
  • Both row levels and both column levels appear at least once, so every expected count is positive. A single cell may still have zero subjects.
  • Repeated rows are separate subjects and each one counts.
  • Report the uncorrected Pearson statistic, with no continuity correction.