Chi-Square Degrees of Freedom for a Two-Way Table

Quick Overview

Write a SQL query that counts the distinct levels of two categorical columns and returns the chi-square degrees of freedom for their two-way table. It tests COUNT(DISTINCT) aggregation, counting levels that actually appear in the data, and why the test of independence uses the product of levels minus one.

Chi-Square Degrees of Freedom for a Two-Way Table

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

The `crosstab_data` table has one row per subject with `row_cat` (levels `'A'`, `'B'`, `'C'`) and `col_cat` (levels `'P'`, `'Q'`, `'R'`, `'S'`). Chi-square degrees of freedom are `(num_rows - 1) * (num_cols - 1)`, where `num_rows` and `num_cols` are the numbers of distinct levels of each attribute. Return one row with `num_rows`, `num_cols` and `df`. ```hint Which levels count The level lists above and the levels that actually appear in the table can differ. Check which ones the example output counts. ``` ### Input Table(s) `crosstab_data` | Column | Type | Meaning | |---|---|---| | row_cat | VARCHAR(5) | Row attribute of the subject (`'A'`, `'B'` or `'C'`) | | col_cat | VARCHAR(5) | Column attribute of the subject (`'P'`, `'Q'`, `'R'` or `'S'`) | 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 `num_rows`, `num_cols`, `df`, in that order, all integers. - `num_rows` is the number of distinct `row_cat` values present in the table, and `num_cols` is the number of distinct `col_cat` values present. - `df` is `(num_rows - 1) * (num_cols - 1)`. - The result has a single row, so no ordering is needed. ### Example Input `crosstab_data`: | row_cat | col_cat | |---|---| | A | P | | A | Q | | B | P | | B | Q | | A | P | | B | Q | Expected output: | num_rows | num_cols | df | |---|---|---| | 2 | 2 | 1 | Only A and B appear in `row_cat` and only P and Q in `col_cat`, so the table is 2 by 2 and `df` is (2 - 1) times (2 - 1), which is 1. The declared levels C, R and S have no rows and are not counted. ### Constraints and Clarifications - The table has at least one row. - `row_cat` and `col_cat` are never NULL. - Count only the levels that occur in the data. A declared level with no subjects is not part of the table. - Repeated rows are separate subjects; they do not add levels.

Overview: Write a SQL query that counts the distinct levels of two categorical columns and returns the chi-square degrees of freedom for their two-way table. It tests COUNT(DISTINCT) aggregation, counting levels that actually appear in the data, and why the test of independence uses the product of levels minus one.

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

The crosstab_data table has one row per subject with row_cat (levels 'A', 'B', 'C') and col_cat (levels 'P', 'Q', 'R', 'S'). Chi-square degrees of freedom are (num_rows - 1) * (num_cols - 1), where num_rows and num_cols are the numbers of distinct levels of each attribute. Return one row with num_rows, num_cols and df.

Input Table(s)

crosstab_data

ColumnTypeMeaning
row_catVARCHAR(5)Row attribute of the subject ('A', 'B' or 'C')
col_catVARCHAR(5)Column attribute of the subject ('P', 'Q', 'R' or 'S')

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 num_rows , num_cols , df , in that order, all integers.
  • num_rows is the number of distinct row_cat values present in the table, and num_cols is the number of distinct col_cat values present.
  • df is (num_rows - 1) * (num_cols - 1) .
  • The result has a single row, so no ordering is needed.

Example

Input crosstab_data:

row_catcol_cat
AP
AQ
BP
BQ
AP
BQ

Expected output:

num_rowsnum_colsdf
221

Only A and B appear in row_cat and only P and Q in col_cat, so the table is 2 by 2 and df is (2 - 1) times (2 - 1), which is 1. The declared levels C, R and S have no rows and are not counted.

Constraints and Clarifications

  • The table has at least one row.
  • row_cat and col_cat are never NULL.
  • Count only the levels that occur in the data. A declared level with no subjects is not part of the table.
  • Repeated rows are separate subjects; they do not add levels.