Mann-Whitney U Statistic

Quick Overview

Write a SQL query that ranks two groups of observations together, gives tied values their average rank, and returns the Mann-Whitney U statistic for one group along with both group sizes. It tests window functions for tie-aware ranking and an understanding of the rank-sum form of U.

Mann-Whitney U Statistic

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Table `obs` records a numeric `value` for groups `group_name` = `'A'` or `'B'`. Rank every `value` across the pooled sample using average (mid) ranks for ties, let $R_1$ be the sum of group A's ranks, and let `n1`, `n2` be the group sizes. Return one row with `u_statistic` = $R_1 - n_1(n_1 + 1)/2$ rounded to 1 decimal, `n1`, and `n2`. ```hint Average ranks in SQL A tied block shares one rank. Look at what `RANK()` returns for each row of the block and how many rows the block has. ``` ### Input Table(s) Table `obs` (no primary key; each row is one observation, and the same group and value can repeat): | Column | Type | Meaning | |---|---|---| | group_name | VARCHAR(5) | Group label, `'A'` or `'B'` | | value | INT | The observed value | ### Output Contract - Exactly one row with the columns `u_statistic`, `n1`, `n2`, in that order. The result has a single row, so no ordering is needed. - `u_statistic` is $R_1 - n_1(n_1 + 1)/2$ rounded to 1 decimal place. - `n1` and `n2` are the integer row counts of groups A and B. ### Example `obs`: | group_name | value | |---|---| | A | 3 | | B | 3 | | B | 1 | | A | 4 | | A | 9 | | B | 13 | | B | 1 | | A | 14 | | A | 1 | | A | 15 | | B | 15 | | B | 11 | Expected result: | u_statistic | n1 | n2 | |---|---|---| | 20.0 | 6 | 6 | Each group has 6 rows. In the pooled ranking the three 1s share rank 2, the two 3s share 4.5 and the two 15s share 11.5, while 4, 9 and 14 get ranks 6, 7 and 10. Group A's ranks are 2, 4.5, 6, 7, 10 and 11.5, which sum to 41, so U = 41 - 21 = 20. ### Constraints and Clarifications - Rank all rows of both groups together in ascending order of `value`. Tied rows, in either group, get the mean of the positions they occupy. - Duplicate rows are separate observations, and each one is ranked. - `group_name` and `value` are never NULL, `group_name` is only `'A'` or `'B'`, and both groups have at least one row. - U can end in .5 when a tie spans both groups, which is why it is rounded to 1 decimal.

Overview: Write a SQL query that ranks two groups of observations together, gives tied values their average rank, and returns the Mann-Whitney U statistic for one group along with both group sizes. It tests window functions for tie-aware ranking and an understanding of the rank-sum form of U.

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

Table obs records a numeric value for groups group_name = 'A' or 'B'. Rank every value across the pooled sample using average (mid) ranks for ties, let R1R_1 be the sum of group A's ranks, and let n1, n2 be the group sizes. Return one row with u_statistic = R1−n1(n1+1)/2R_1 - n_1(n_1 + 1)/2 rounded to 1 decimal, n1, and n2.

Input Table(s)

Table obs (no primary key; each row is one observation, and the same group and value can repeat):

ColumnTypeMeaning
group_nameVARCHAR(5)Group label, 'A' or 'B'
valueINTThe observed value

Output Contract

  • Exactly one row with the columns u_statistic , n1 , n2 , in that order. The result has a single row, so no ordering is needed.
  • u_statistic is R1−n1(n1+1)/2R_1 - n_1(n_1 + 1)/2 rounded to 1 decimal place.
  • n1 and n2 are the integer row counts of groups A and B.

Example

obs:

group_namevalue
A3
B3
B1
A4
A9
B13
B1
A14
A1
A15
B15
B11

Expected result:

u_statisticn1n2
20.066

Each group has 6 rows. In the pooled ranking the three 1s share rank 2, the two 3s share 4.5 and the two 15s share 11.5, while 4, 9 and 14 get ranks 6, 7 and 10. Group A's ranks are 2, 4.5, 6, 7, 10 and 11.5, which sum to 41, so U = 41 - 21 = 20.

Constraints and Clarifications

  • Rank all rows of both groups together in ascending order of value . Tied rows, in either group, get the mean of the positions they occupy.
  • Duplicate rows are separate observations, and each one is ranked.
  • group_name and value are never NULL, group_name is only 'A' or 'B' , and both groups have at least one row.
  • U can end in .5 when a tie spans both groups, which is why it is rounded to 1 decimal.