Build a full one-way ANOVA table (SS, df, MS, F) in SQL

Quick Overview

Write a PostgreSQL query that turns a table of grouped scores into a complete one-way ANOVA table with sums of squares, degrees of freedom, mean squares and the F statistic. It tests multi-step aggregation, a fixed row order across a union, and rounding only at the final step.

Build a full one-way ANOVA table (SS, df, MS, F) in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

The `scores` table records a numeric `value` per subject, and each subject belongs to one group `grp`, which is `'A'`, `'B'` or `'C'`. Produce a one-way ANOVA table with columns `src`, `ss`, `df`, `ms`, `f` and three rows labelled `'Between'`, `'Within'` and `'Total'`. Let $n_g$ and $\bar{x}_g$ be the row count and mean `value` of group $g$, $\bar{x}$ the mean of `value` over all rows, $N$ the number of rows and $k$ the number of groups. The rows are defined as follows: - Between: $SSB = \sum_g n_g (\bar{x}_g - \bar{x})^2$ with `df` = k - 1. - Within: $SSW = \sum_g \sum_{i \in g} (x_i - \bar{x}_g)^2$ with `df` = N - k. - Total: $SST = SSB + SSW$ with `df` = N - 1. - `ms` = `ss` / `df` on the Between and Within rows, and `f` = MSB / MSW on the Between row. `ms` and `f` are NULL everywhere else. - Round `ss`, `ms` and `f` to 4 decimal places. ### 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 Exactly three rows, in the order Between, Within, Total, with these columns: | Column | Type | Meaning | |---|---|---| | src | TEXT | 'Between', 'Within' or 'Total' (unique per row) | | ss | NUMERIC | Sum of squares, rounded to 4 decimal places | | df | INTEGER | Degrees of freedom, not rounded | | ms | NUMERIC | Mean square rounded to 4 decimal places; NULL on the Total row | | f | NUMERIC | F statistic rounded to 4 decimal places on the Between row; NULL on the other two | ### Example Input `scores`: | grp | value | |---|---| | A | 16.5 | | C | 29.2 | | C | 25.9 | | A | 17 | | C | 22.3 | | A | 10.4 | | B | 21.6 | | B | 19.3 | | C | 25.5 | | C | 26.4 | | B | 17.2 | | C | 26.6 | | B | 15.6 | | A | 17.3 | Output: | src | ss | df | ms | f | |---|---|---|---|---| | Between | 304.7527 | 2 | 152.3764 | 21.6623 | | Within | 77.3758 | 11 | 7.0342 | NULL | | Total | 382.1286 | 13 | NULL | NULL | Groups A, B and C have 4, 4 and 6 rows with means 15.3, 18.425 and 25.98333, and the grand mean is 290.8 / 14 = 20.77143. That gives SSB = 304.75274 and SSW = 32.34 + 20.3275 + 24.70833 = 77.37583. With k = 3 and N = 14, the degrees of freedom are 2, 11 and 13, so MSB = 152.37637, MSW = 7.03417 and F = 21.66232. ### Constraints and Clarifications - Neither column contains NULL. - $k$ is the number of distinct `grp` values present in the table, and $N$ counts every row. - Every input has at least two groups, more rows than groups, and some variation inside at least one group, so SSW is positive and every `ms` and `f` value is defined. - Identical rows are separate subjects. Each one counts toward $n_g$, $N$, the means and the sums. - Compute the Total row, the mean squares and F from unrounded values, and round only the final cells. ```hint One row of totals first Get SSB, SSW, N and k into a single row before building the table. Every cell is then simple arithmetic on those four numbers. ```

Overview: Write a PostgreSQL query that turns a table of grouped scores into a complete one-way ANOVA table with sums of squares, degrees of freedom, mean squares and the F statistic. It tests multi-step aggregation, a fixed row order across a union, and rounding only at the final step.

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

The scores table records a numeric value per subject, and each subject belongs to one group grp, which is 'A', 'B' or 'C'. Produce a one-way ANOVA table with columns src, ss, df, ms, f and three rows labelled 'Between', 'Within' and 'Total'.

Let ngn_g and xˉg\bar{x}_g be the row count and mean value of group gg, xˉ\bar{x} the mean of value over all rows, NN the number of rows and kk the number of groups. The rows are defined as follows:

  • Between: SSB=∑gng(xˉg−xˉ)2SSB = \sum_g n_g (\bar{x}_g - \bar{x})^2 with df = k - 1.
  • Within: SSW=∑g∑i∈g(xi−xˉg)2SSW = \sum_g \sum_{i \in g} (x_i - \bar{x}_g)^2 with df = N - k.
  • Total: SST=SSB+SSWSST = SSB + SSW with df = N - 1.
  • ms = ss / df on the Between and Within rows, and f = MSB / MSW on the Between row. ms and f are NULL everywhere else.
  • Round ss , ms and f to 4 decimal places.

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

Exactly three rows, in the order Between, Within, Total, with these columns:

ColumnTypeMeaning
srcTEXT'Between', 'Within' or 'Total' (unique per row)
ssNUMERICSum of squares, rounded to 4 decimal places
dfINTEGERDegrees of freedom, not rounded
msNUMERICMean square rounded to 4 decimal places; NULL on the Total row
fNUMERICF statistic rounded to 4 decimal places on the Between row; NULL on the other two

Example

Input scores:

grpvalue
A16.5
C29.2
C25.9
A17
C22.3
A10.4
B21.6
B19.3
C25.5
C26.4
B17.2
C26.6
B15.6
A17.3

Output:

srcssdfmsf
Between304.75272152.376421.6623
Within77.3758117.0342NULL
Total382.128613NULLNULL

Groups A, B and C have 4, 4 and 6 rows with means 15.3, 18.425 and 25.98333, and the grand mean is 290.8 / 14 = 20.77143. That gives SSB = 304.75274 and SSW = 32.34 + 20.3275 + 24.70833 = 77.37583. With k = 3 and N = 14, the degrees of freedom are 2, 11 and 13, so MSB = 152.37637, MSW = 7.03417 and F = 21.66232.

Constraints and Clarifications

  • Neither column contains NULL.
  • kk is the number of distinct grp values present in the table, and NN counts every row.
  • Every input has at least two groups, more rows than groups, and some variation inside at least one group, so SSW is positive and every ms and f value is defined.
  • Identical rows are separate subjects. Each one counts toward ngn_g , NN , the means and the sums.
  • Compute the Total row, the mean squares and F from unrounded values, and round only the final cells.