Compute the one-way ANOVA F statistic and its degrees of freedom in SQL

Quick Overview

Write a PostgreSQL query that runs a one-way ANOVA on grouped scores and returns the F statistic with its between-group and within-group degrees of freedom. It tests grouped or window aggregation, weighting by group size, and keeping full precision until the final rounding.

Compute the one-way ANOVA F statistic and its degrees of freedom in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Table `scores` records a numeric `value` per subject, and each subject belongs to one group `grp`, which is `'A'`, `'B'` or `'C'`. Perform a one-way ANOVA: $$SSB = \sum_g n_g (\bar{x}_g - \bar{x})^2, \qquad SSW = \sum_g \sum_{i \in g} (x_i - \bar{x}_g)^2, \qquad F = \frac{SSB / (k - 1)}{SSW / (N - k)}$$ Here $n_g$ and $\bar{x}_g$ are the row count and mean `value` of group $g$, $\bar{x}$ is the mean of `value` over all rows, $k$ is the number of groups and $N$ the total number of rows. Return one row with `f_statistic` rounded to 4 decimal places, `df_between` = k - 1 and `df_within` = N - k. ### Input Table(s) `scores` | Column | Type | Meaning | |---|---|---| | grp | VARCHAR(5) | Group label: 'A', 'B' or 'C' | | value | DECIMAL(10,4) | The subject's measured value | There is no primary key. Each row is one subject's observation, and two rows can be identical. ### Output Contract One row with exactly these columns, in this order: | Column | Type | Meaning | |---|---|---| | f_statistic | NUMERIC | The ANOVA F statistic, rounded to 4 decimal places | | df_between | INTEGER | k - 1 | | df_within | INTEGER | N - k | The result always has exactly one row, so it needs no ordering. ### Example Input `scores`: | grp | value | |---|---| | C | 25.4 | | B | 22.9 | | B | 17.1 | | C | 20.1 | | A | 10.2 | | C | 28.7 | | A | 14.1 | | A | 12.7 | | A | 11.3 | | C | 22.2 | | C | 25.7 | | B | 17.9 | | B | 22.6 | | A | 16.9 | | B | 21.2 | Output: | f_statistic | df_between | df_within | |---|---|---| | 19.8267 | 2 | 12 | Each group has 5 rows, with means 13.04 (A), 20.34 (B) and 24.42 (C), and the grand mean is 289 / 15 = 19.26667. That gives SSB = 332.40133 and SSW = 27.232 + 28.852 + 44.508 = 100.592. With k = 3 and N = 15, F = (332.40133 / 2) / (100.592 / 12) = 166.20067 / 8.38267 = 19.82671. ### Constraints and Clarifications - Neither column contains NULL. - $k$ is the number of distinct `grp` values present, and $N$ counts every row. - Group sizes can differ. The grand mean is the mean over all rows, which equals the plain average of the group means only when every group has the same number of rows. - Identical rows are separate subjects. Each one counts toward $n_g$, $N$, the means and both sums. - Every input has at least two groups, more rows than groups, and some variation inside at least one group, so SSW is positive and F is defined. - Round only the final F. The degrees of freedom are integers. ```hint Weighting by group size SSB weights each group by its row count. If you build it from a one-row-per-group table, carry the count along; if you sum over rows, the weight comes for free. ```

Overview: Write a PostgreSQL query that runs a one-way ANOVA on grouped scores and returns the F statistic with its between-group and within-group degrees of freedom. It tests grouped or window aggregation, weighting by group size, and keeping full precision until the final rounding.

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

Table scores records a numeric value per subject, and each subject belongs to one group grp, which is 'A', 'B' or 'C'. Perform a one-way ANOVA:

SSB=∑gng(xˉg−xˉ)2,SSW=∑g∑i∈g(xi−xˉg)2,F=SSB/(k−1)SSW/(N−k)SSB = \sum_g n_g (\bar{x}_g - \bar{x})^2, \qquad SSW = \sum_g \sum_{i \in g} (x_i - \bar{x}_g)^2, \qquad F = \frac{SSB / (k - 1)}{SSW / (N - k)}

Here ngn_g and xˉg\bar{x}_g are the row count and mean value of group gg, xˉ\bar{x} is the mean of value over all rows, kk is the number of groups and NN the total number of rows. Return one row with f_statistic rounded to 4 decimal places, df_between = k - 1 and df_within = N - k.

Input Table(s)

scores

ColumnTypeMeaning
grpVARCHAR(5)Group label: 'A', 'B' or 'C'
valueDECIMAL(10,4)The subject's measured value

There is no primary key. Each row is one subject's observation, and two rows can be identical.

Output Contract

One row with exactly these columns, in this order:

ColumnTypeMeaning
f_statisticNUMERICThe ANOVA F statistic, rounded to 4 decimal places
df_betweenINTEGERk - 1
df_withinINTEGERN - k

The result always has exactly one row, so it needs no ordering.

Example

Input scores:

grpvalue
C25.4
B22.9
B17.1
C20.1
A10.2
C28.7
A14.1
A12.7
A11.3
C22.2
C25.7
B17.9
B22.6
A16.9
B21.2

Output:

f_statisticdf_betweendf_within
19.8267212

Each group has 5 rows, with means 13.04 (A), 20.34 (B) and 24.42 (C), and the grand mean is 289 / 15 = 19.26667. That gives SSB = 332.40133 and SSW = 27.232 + 28.852 + 44.508 = 100.592. With k = 3 and N = 15, F = (332.40133 / 2) / (100.592 / 12) = 166.20067 / 8.38267 = 19.82671.

Constraints and Clarifications

  • Neither column contains NULL.
  • kk is the number of distinct grp values present, and NN counts every row.
  • Group sizes can differ. The grand mean is the mean over all rows, which equals the plain average of the group means only when every group has the same number of rows.
  • Identical rows are separate subjects. Each one counts toward ngn_g , NN , the means and both sums.
  • Every input has at least two groups, more rows than groups, and some variation inside at least one group, so SSW is positive and F is defined.
  • Round only the final F. The degrees of freedom are integers.