Compute expected contingency table cell counts under independence in SQL

Quick Overview

Write a PostgreSQL query that turns respondent-level survey rows into the expected cell counts of a 2x2 contingency table under independence. It tests marginal totals, producing every cell even when no respondent falls into it, and exact numeric division with rounding.

Compute expected contingency table cell counts under independence in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Table `survey` has one row per respondent. Each respondent has a row category `row_cat` (`'A'` or `'B'`) and a column category `col_cat` (`'X'` or `'Y'`), so the table describes a 2x2 contingency table. Under independence, a cell's expected count is the row total times the column total divided by the grand total: $$E_{ij} = \frac{R_i \cdot C_j}{N}$$ Here $R_i$ is the number of respondents in row category $i$, $C_j$ is the number in column category $j$, and $N$ is the number of rows in the table. Return one row per cell with `row_cat`, `col_cat` and `expected` rounded to 4 decimals. ```hint Where the cells come from Compute the totals before you build the cells. A cell that no respondent falls into still has an expected count. ``` ### Input Table(s) `survey` | Column | Type | Meaning | |---|---|---| | `row_cat` | VARCHAR(5) | Row category of the respondent, `'A'` or `'B'` | | `col_cat` | VARCHAR(5) | Column category of the respondent, `'X'` or `'Y'` | The table has no key. Each row is one respondent, so identical rows are normal. ### Output Contract - Columns, in this order: `row_cat`, `col_cat`, `expected`. - `expected` is the cell's expected count, rounded to 4 decimal places. - One row for every pairing of a `row_cat` value with a `col_cat` value that appears in the table, including a pairing that no respondent has. - Sort by `row_cat` ascending, then `col_cat` ascending. Each pair appears once, so this order is unique. ### Example `survey` (17 rows): | row_cat | col_cat | |---|---| | A | Y | | A | Y | | B | X | | B | X | | A | Y | | A | X | | A | Y | | A | Y | | B | Y | | B | Y | | B | X | | B | X | | A | X | | A | X | | A | X | | B | X | | B | Y | The row totals are A = 9 and B = 8, the column totals are X = 9 and Y = 8, and $N = 17$. Expected result: | row_cat | col_cat | expected | |---|---|---| | A | X | 4.7647 | | A | Y | 4.2353 | | B | X | 4.2353 | | B | Y | 3.7647 | Cell (A, X) is ${9 \cdot 9 / 17 = 81/17 \approx 4.7647}$, and cell (B, Y) is ${8 \cdot 8 / 17 = 64/17 \approx 3.7647}$. ### Constraints and Clarifications - `row_cat` and `col_cat` are never NULL. - Count every row. Duplicate rows are separate respondents. - Use exact division and round only the final value. - Two cells can have the same expected count (A, Y and B, X above). The sort on the category pair keeps the order unique. - If `survey` is empty, return no rows.

Overview: Write a PostgreSQL query that turns respondent-level survey rows into the expected cell counts of a 2x2 contingency table under independence. It tests marginal totals, producing every cell even when no respondent falls into it, and exact numeric division with rounding.

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

Table survey has one row per respondent. Each respondent has a row category row_cat ('A' or 'B') and a column category col_cat ('X' or 'Y'), so the table describes a 2x2 contingency table. Under independence, a cell's 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}

Here RiR_i is the number of respondents in row category ii, CjC_j is the number in column category jj, and NN is the number of rows in the table. Return one row per cell with row_cat, col_cat and expected rounded to 4 decimals.

Input Table(s)

survey

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

The table has no key. Each row is one respondent, so identical rows are normal.

Output Contract

  • Columns, in this order: row_cat , col_cat , expected .
  • expected is the cell's expected count, rounded to 4 decimal places.
  • One row for every pairing of a row_cat value with a col_cat value that appears in the table, including a pairing that no respondent has.
  • Sort by row_cat ascending, then col_cat ascending. Each pair appears once, so this order is unique.

Example

survey (17 rows):

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

The row totals are A = 9 and B = 8, the column totals are X = 9 and Y = 8, and N=17N = 17.

Expected result:

row_catcol_catexpected
AX4.7647
AY4.2353
BX4.2353
BY3.7647

Cell (A, X) is 9⋅9/17=81/17≈4.7647{9 \cdot 9 / 17 = 81/17 \approx 4.7647}, and cell (B, Y) is 8⋅8/17=64/17≈3.7647{8 \cdot 8 / 17 = 64/17 \approx 3.7647}.

Constraints and Clarifications

  • row_cat and col_cat are never NULL.
  • Count every row. Duplicate rows are separate respondents.
  • Use exact division and round only the final value.
  • Two cells can have the same expected count (A, Y and B, X above). The sort on the category pair keeps the order unique.
  • If survey is empty, return no rows.