Mood's Median Test Chi-Square in SQL Without PERCENTILE_CONT

Quick Overview

Implement Mood's median test in SQL: compute the grand median of two groups without PERCENTILE_CONT, classify each value as above or not above it, build the 2x2 contingency table and return Pearson's chi-square statistic. It tests window functions, median logic for odd and even counts, and chi-square arithmetic.

Mood's Median Test Chi-Square in SQL Without PERCENTILE_CONT

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Table `vals` holds numeric observations `value` for two groups, `group_name` = `'A'` or `'B'`. Run Mood's median test on them and return one row with `chi_square`, rounded to 4 decimals, and `df` = 1. - Compute the grand median of all values without using `PERCENTILE_CONT`. - Classify each row as above (`value` strictly greater than the grand median) or not-above. - Build the 2x2 contingency table of `group_name` by above/not-above. - Compute the Pearson chi-square statistic `SUM((observed - expected)^2 / expected)` over the four cells, with `expected = row_total * col_total / grand_total`. ```hint Finding the middle Number the sorted values and pick two positions that land on the same row when the count is odd and on the two middle rows when it is even. One formula then covers both cases. ``` ### Input Table(s) Table `vals` (no key; values can repeat): | Column | Type | Meaning | |---|---|---| | `group_name` | VARCHAR(5) | Group: `'A'` or `'B'` | | `value` | NUMERIC(8,2) | The observation | ### Output Contract Exactly one row with these columns, in this order: | Column | Definition | Rounding | |---|---|---| | `chi_square` | Pearson chi-square statistic of the 2x2 table | 4 decimals | | `df` | Degrees of freedom, always 1 | integer | The grand median is the middle value of all rows sorted by `value`, or the average of the two middle values when the row count is even. It is taken over both groups together. With $O$ the observed count of a cell and $E$ its expected count: $$ \chi^2 = \sum_{\text{4 cells}} \frac{(O - E)^2}{E}, \qquad E = \frac{\text{row total} \times \text{column total}}{\text{grand total}} $$ No continuity correction is applied. The result is a single row, so no ordering is needed. ### Example Input `vals`: | group_name | value | |---|---| | B | 55.91 | | B | 46.51 | | B | 84.31 | | A | 43.63 | | A | 27.47 | | B | 64.68 | | B | 63.97 | | A | 52.21 | | A | 51.95 | | A | 54.48 | | A | 27.63 | | B | 57.92 | | A | 48.27 | | B | 76.69 | Expected output: | chi_square | df | |---|---| | 7.1429 | 1 | Sorted, the 7th and 8th of the 14 values are 52.21 and 54.48, so the grand median is 53.345. Group A has one value above it (54.48) and six not above. Group B has six above and one not above (46.51). Every row and column total is 7, so each expected count is 3.5, and $\chi^2 = {4 \cdot 2.5^2 / 3.5} \approx 7.1429$. ### Constraints and Clarifications - `group_name` is always `'A'` or `'B'`, and both groups have rows. - `value` is never NULL. - Values can repeat. A value equal to the grand median counts as not-above. - Every expected count is positive: both groups are present and at least one value is strictly above the grand median.

Overview: Implement Mood's median test in SQL: compute the grand median of two groups without PERCENTILE_CONT, classify each value as above or not above it, build the 2x2 contingency table and return Pearson's chi-square statistic. It tests window functions, median logic for odd and even counts, and chi-square arithmetic.

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

Table vals holds numeric observations value for two groups, group_name = 'A' or 'B'. Run Mood's median test on them and return one row with chi_square, rounded to 4 decimals, and df = 1.

  • Compute the grand median of all values without using PERCENTILE_CONT .
  • Classify each row as above ( value strictly greater than the grand median) or not-above.
  • Build the 2x2 contingency table of group_name by above/not-above.
  • Compute the Pearson chi-square statistic SUM((observed - expected)^2 / expected) over the four cells, with expected = row_total * col_total / grand_total .

Input Table(s)

Table vals (no key; values can repeat):

ColumnTypeMeaning
group_nameVARCHAR(5)Group: 'A' or 'B'
valueNUMERIC(8,2)The observation

Output Contract

Exactly one row with these columns, in this order:

ColumnDefinitionRounding
chi_squarePearson chi-square statistic of the 2x2 table4 decimals
dfDegrees of freedom, always 1integer

The grand median is the middle value of all rows sorted by value, or the average of the two middle values when the row count is even. It is taken over both groups together. With OO the observed count of a cell and EE its expected count:

χ2=∑4 cells(O−E)2E,E=row total×column totalgrand total\chi^2 = \sum_{\text{4 cells}} \frac{(O - E)^2}{E}, \qquad E = \frac{\text{row total} \times \text{column total}}{\text{grand total}}

No continuity correction is applied. The result is a single row, so no ordering is needed.

Example

Input vals:

group_namevalue
B55.91
B46.51
B84.31
A43.63
A27.47
B64.68
B63.97
A52.21
A51.95
A54.48
A27.63
B57.92
A48.27
B76.69

Expected output:

chi_squaredf
7.14291

Sorted, the 7th and 8th of the 14 values are 52.21 and 54.48, so the grand median is 53.345. Group A has one value above it (54.48) and six not above. Group B has six above and one not above (46.51). Every row and column total is 7, so each expected count is 3.5, and χ2=4⋅2.52/3.5≈7.1429\chi^2 = {4 \cdot 2.5^2 / 3.5} \approx 7.1429.

Constraints and Clarifications

  • group_name is always 'A' or 'B' , and both groups have rows.
  • value is never NULL.
  • Values can repeat. A value equal to the grand median counts as not-above.
  • Every expected count is positive: both groups are present and at least one value is strictly above the grand median.