Compute the Kruskal-Wallis H statistic and test decision in SQL

Quick Overview

Write a PostgreSQL query that runs a Kruskal-Wallis test on grouped observations: pool the values, give ties average ranks, compute the H statistic, and compare it with a chi-square critical value from a lookup table. It tests window-function ranking, tie handling and numeric precision in SQL.

Compute the Kruskal-Wallis H statistic and test decision in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

The table `obs` records one observation per row: an integer `value` for a group `group_name`, which is `'A'`, `'B'` or `'C'`. Run a Kruskal-Wallis test across the groups in a single query. Pool all the values and rank them from smallest (rank 1) to largest, giving tied values the average (mid) rank of the positions they occupy. Then compute $$ H = \frac{12}{N(N+1)} \sum_i \frac{R_i^2}{n_i} - 3(N+1) $$ where $R_i$ is the rank sum of group $i$, $n_i$ is the number of rows in group $i$, and $N$ is the total number of rows. Compare $H$ against the chi-square critical value stored in `critical_values` for the test's degrees of freedom at $\alpha = 0.05$. ```hint Ranking ties Window functions can give you both the position where a block of tied values starts and how many rows the block holds. Those two numbers are enough to get the block's average rank. ``` ### Input Table(s) `obs` | Column | Type | Meaning | |---|---|---| | group_name | VARCHAR(5) | Group label, one of `'A'`, `'B'`, `'C'` | | value | INT | The observed value | No primary key. Two rows with the same group and value are two separate observations. `critical_values` | Column | Type | Meaning | |---|---|---| | df | INT | Degrees of freedom | | alpha | DECIMAL(4,3) | Significance level | | critical_value | DECIMAL(8,4) | Chi-square critical value for that `df` and `alpha` | Primary key: (`df`, `alpha`). ### Output Contract Return exactly one row with these columns, in this order: | Column | Meaning | |---|---| | h_statistic | $H$ rounded to 4 decimal places | | df | Number of groups present in `obs`, minus 1 | | critical_value | The `critical_value` for that `df` at `alpha = 0.05`, as stored | | decision | `'reject'` when $H$ is strictly greater than `critical_value`, otherwise `'accept'` | Make the comparison with the unrounded $H$. The result is a single row, so no ordering is needed. ### Example `obs` | group_name | value | |---|---| | C | 1 | | A | 16 | | B | 5 | | A | 17 | | B | 10 | | C | 13 | | A | 5 | | B | 11 | | C | 4 | | B | 6 | | A | 13 | | C | 12 | `critical_values` | df | alpha | critical_value | |---|---|---| | 2 | 0.05 | 5.991 | | 2 | 0.01 | 9.21 | | 3 | 0.05 | 7.815 | | 3 | 0.01 | 11.345 | Expected output: | h_statistic | df | critical_value | decision | |---|---|---|---| | 2.8942 | 2 | 5.991 | accept | The two 5s sit at positions 3 and 4, so both get rank 3.5. The two 13s sit at positions 9 and 10, so both get rank 9.5. Three groups give 2 degrees of freedom, and 2.8942 is below 5.991. ### Constraints and Clarifications - Ranks come from the pooled values of all groups together, so a tie between two groups gives both rows the same average rank. - Use the formula exactly as written. Do not divide by a tie-correction factor. - `df` counts the groups that have at least one row in `obs`. At least two groups are present. - `group_name` and `value` are never NULL. - `critical_values` always has a row for the needed `df` at `alpha = 0.05`. Rows for other `alpha` levels must not affect the result. - `'accept'` means the test fails to reject the null hypothesis that all groups come from the same distribution.

Overview: Write a PostgreSQL query that runs a Kruskal-Wallis test on grouped observations: pool the values, give ties average ranks, compute the H statistic, and compare it with a chi-square critical value from a lookup table. It tests window-function ranking, tie handling and numeric precision in SQL.

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

The table obs records one observation per row: an integer value for a group group_name, which is 'A', 'B' or 'C'. Run a Kruskal-Wallis test across the groups in a single query.

Pool all the values and rank them from smallest (rank 1) to largest, giving tied values the average (mid) rank of the positions they occupy. Then compute

H=12N(N+1)∑iRi2ni−3(N+1)H = \frac{12}{N(N+1)} \sum_i \frac{R_i^2}{n_i} - 3(N+1)

where RiR_i is the rank sum of group ii, nin_i is the number of rows in group ii, and NN is the total number of rows. Compare HH against the chi-square critical value stored in critical_values for the test's degrees of freedom at α=0.05\alpha = 0.05.

Input Table(s)

obs

ColumnTypeMeaning
group_nameVARCHAR(5)Group label, one of 'A', 'B', 'C'
valueINTThe observed value

No primary key. Two rows with the same group and value are two separate observations.

critical_values

ColumnTypeMeaning
dfINTDegrees of freedom
alphaDECIMAL(4,3)Significance level
critical_valueDECIMAL(8,4)Chi-square critical value for that df and alpha

Primary key: (df, alpha).

Output Contract

Return exactly one row with these columns, in this order:

ColumnMeaning
h_statisticHH rounded to 4 decimal places
dfNumber of groups present in obs, minus 1
critical_valueThe critical_value for that df at alpha = 0.05, as stored
decision'reject' when HH is strictly greater than critical_value, otherwise 'accept'

Make the comparison with the unrounded HH. The result is a single row, so no ordering is needed.

Example

obs

group_namevalue
C1
A16
B5
A17
B10
C13
A5
B11
C4
B6
A13
C12

critical_values

dfalphacritical_value
20.055.991
20.019.21
30.057.815
30.0111.345

Expected output:

h_statisticdfcritical_valuedecision
2.894225.991accept

The two 5s sit at positions 3 and 4, so both get rank 3.5. The two 13s sit at positions 9 and 10, so both get rank 9.5. Three groups give 2 degrees of freedom, and 2.8942 is below 5.991.

Constraints and Clarifications

  • Ranks come from the pooled values of all groups together, so a tie between two groups gives both rows the same average rank.
  • Use the formula exactly as written. Do not divide by a tie-correction factor.
  • df counts the groups that have at least one row in obs . At least two groups are present.
  • group_name and value are never NULL.
  • critical_values always has a row for the needed df at alpha = 0.05 . Rows for other alpha levels must not affect the result.
  • 'accept' means the test fails to reject the null hypothesis that all groups come from the same distribution.