Chi-Square Goodness-of-Fit Test Against a Uniform Distribution in SQL

Quick Overview

Write a SQL query that tests whether event counts per category fit a uniform distribution, returning the chi-square goodness-of-fit statistic and its degrees of freedom. It tests grouped aggregation, combining per-group and overall totals, and how the chi-square test is set up.

Chi-Square Goodness-of-Fit Test Against a Uniform Distribution in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Table `observations` has one row per event with a `category` (`'A'`, `'B'`, `'C'` or `'D'`). Test the category counts against a uniform distribution: each category's expected count is total / k, where total is the number of events and k is the number of distinct categories. Return one row with the chi-square statistic and its degrees of freedom. ```hint Order of operations Get the per-category counts first. The outer sum runs over those grouped rows, one per category, not over the raw events. ``` ### Input Table(s) `observations` | Column | Type | Meaning | |---|---|---| | category | VARCHAR(5) | Category of the event: `'A'`, `'B'`, `'C'` or `'D'` | There is no primary key. Each row is one event. ### Output Contract Exactly one row with these columns, in this order: | Column | Definition | |---|---| | chi_square | SUM((observed - expected)^2 / expected) over the categories, rounded to 4 decimals | | df | k - 1 | The result is a single row, so no ordering applies. ### Example `observations` | category | |---| | C | | B | | C | | D | | A | | D | | A | | A | | D | | B | | C | | A | | A | | D | | C | | D | | B | | B | | B | Expected result: | chi_square | df | |---|---| | 0.1579 | 3 | There are 19 events in 4 categories, so each expected count is 4.75. A, B and D each appear 5 times and C appears 4 times. ### Constraints and Clarifications - `category` is never NULL. - `observations` has at least one row. - k counts only the categories that appear at least once. A category with no events is left out of both the sum and k. - Duplicate rows are separate events and all count.

Overview: Write a SQL query that tests whether event counts per category fit a uniform distribution, returning the chi-square goodness-of-fit statistic and its degrees of freedom. It tests grouped aggregation, combining per-group and overall totals, and how the chi-square test is set up.

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

Table observations has one row per event with a category ('A', 'B', 'C' or 'D'). Test the category counts against a uniform distribution: each category's expected count is total / k, where total is the number of events and k is the number of distinct categories. Return one row with the chi-square statistic and its degrees of freedom.

Input Table(s)

observations

ColumnTypeMeaning
categoryVARCHAR(5)Category of the event: 'A', 'B', 'C' or 'D'

There is no primary key. Each row is one event.

Output Contract

Exactly one row with these columns, in this order:

ColumnDefinition
chi_squareSUM((observed - expected)^2 / expected) over the categories, rounded to 4 decimals
dfk - 1

The result is a single row, so no ordering applies.

Example

observations

category
C
B
C
D
A
D
A
A
D
B
C
A
A
D
C
D
B
B
B

Expected result:

chi_squaredf
0.15793

There are 19 events in 4 categories, so each expected count is 4.75. A, B and D each appear 5 times and C appears 4 times.

Constraints and Clarifications

  • category is never NULL.
  • observations has at least one row.
  • k counts only the categories that appear at least once. A category with no events is left out of both the sum and k.
  • Duplicate rows are separate events and all count.