Compute Mann-Whitney U statistics from pooled rank sums in SQL

Quick Overview

Write a SQL query that ranks the pooled observations of two groups, sums the ranks per group, and turns each rank sum into a Mann-Whitney U statistic. It tests window-function ranking over a pooled sample, grouping, and how rank sums relate to the U test.

Compute Mann-Whitney U statistics from pooled rank sums in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

The `obs` table records a continuous `value` for two groups (`group_name` is `'A'` or `'B'`). Rank every `value` across the pooled sample from lowest to highest with `RANK()`. Return one row per group with `group_name`, `rank_sum` (the sum of its members' ranks), and `u_statistic`, rounded to 1 decimal: $$ U = R - \frac{n(n+1)}{2} $$ where $R$ is the group's `rank_sum` and $n$ is the group's size. ```hint Order of operations Assign the ranks before you group. Ranks computed after grouping, or inside each group, lose the pooled ordering. ``` ### Input Table(s) `obs` | Column | Type | Meaning | |---|---|---| | group_name | VARCHAR(5) | Group label, `'A'` or `'B'` | | value | DECIMAL(8,2) | The observed value | No primary key. Two rows with the same group and value are two separate observations. ### Output Contract Return one row per group with these columns, in this order: | Column | Meaning | |---|---| | group_name | The group label | | rank_sum | Sum of the pooled ranks of the group's rows | | u_statistic | $U$ for the group, rounded to 1 decimal place | Order the rows by `group_name` ascending. ### Example `obs` | group_name | value | |---|---| | A | 45.29 | | B | 38.23 | | B | 95.29 | | A | 84.41 | | A | 89.11 | | A | 69.88 | | B | 19.34 | | B | 31.81 | | A | 64.17 | | B | 48.91 | | A | 61.55 | | B | 48.56 | Expected output: | group_name | rank_sum | u_statistic | |---|---|---| | A | 49 | 28.0 | | B | 29 | 8.0 | B holds pooled positions 1, 2, 3, 5, 6 and 12, so its rank sum is 29 and its $U$ is 29 - 21 = 8. A holds the other six positions, which sum to 49. ### Constraints and Clarifications - Ranks come from all rows of both groups together. - Use `RANK()` as stated: tied values share the lowest position of their block, with no averaging. The values are continuous, so ties are rare. - `group_name` and `value` are never NULL, and each group has at least one row. - Duplicate rows all count, both in the ranking and in $n$.

Overview: Write a SQL query that ranks the pooled observations of two groups, sums the ranks per group, and turns each rank sum into a Mann-Whitney U statistic. It tests window-function ranking over a pooled sample, grouping, and how rank sums relate to the U test.

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

The obs table records a continuous value for two groups (group_name is 'A' or 'B'). Rank every value across the pooled sample from lowest to highest with RANK(). Return one row per group with group_name, rank_sum (the sum of its members' ranks), and u_statistic, rounded to 1 decimal:

U=R−n(n+1)2U = R - \frac{n(n+1)}{2}

where RR is the group's rank_sum and nn is the group's size.

Input Table(s)

obs

ColumnTypeMeaning
group_nameVARCHAR(5)Group label, 'A' or 'B'
valueDECIMAL(8,2)The observed value

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

Output Contract

Return one row per group with these columns, in this order:

ColumnMeaning
group_nameThe group label
rank_sumSum of the pooled ranks of the group's rows
u_statisticUU for the group, rounded to 1 decimal place

Order the rows by group_name ascending.

Example

obs

group_namevalue
A45.29
B38.23
B95.29
A84.41
A89.11
A69.88
B19.34
B31.81
A64.17
B48.91
A61.55
B48.56

Expected output:

group_namerank_sumu_statistic
A4928.0
B298.0

B holds pooled positions 1, 2, 3, 5, 6 and 12, so its rank sum is 29 and its UU is 29 - 21 = 8. A holds the other six positions, which sum to 49.

Constraints and Clarifications

  • Ranks come from all rows of both groups together.
  • Use RANK() as stated: tied values share the lowest position of their block, with no averaging. The values are continuous, so ties are rare.
  • group_name and value are never NULL, and each group has at least one row.
  • Duplicate rows all count, both in the ranking and in nn .