Compute ANOVA between-group and within-group sums of squares in SQL

Quick Overview

Write a PostgreSQL query that computes the between-group and within-group sums of squares of a one-way ANOVA from a table of grouped scores. It tests grouped aggregates or window averages, correct use of the grand mean with unequal group sizes, and exact numeric rounding.

Compute ANOVA between-group and within-group sums of squares 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'`. Compute the between-group sum of squares (SSB) and the within-group sum of squares (SSW): $$SSB = \sum_{g} n_g \left(\bar{x}_g - \bar{x}\right)^2$$ $$SSW = \sum_{g} \sum_{i \in g} \left(x_i - \bar{x}_g\right)^2$$ Here $n_g$ and $\bar{x}_g$ are the row count and mean `value` of group $g$, $x_i$ is one row's `value`, and $\bar{x}$ is the grand mean, the mean of `value` over all rows. Return one row with `ssb` and `ssw`, each rounded 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 One row with exactly these columns, in this order: | Column | Type | Meaning | |---|---|---| | ssb | NUMERIC | Between-group sum of squares, rounded to 4 decimal places | | ssw | NUMERIC | Within-group sum of squares, rounded to 4 decimal places | The result always has exactly one row, so it needs no ordering. ### Example Input `scores`: | grp | value | |---|---| | A | 12.7 | | B | 21.9 | | C | 20.9 | | C | 25 | | B | 16.2 | | C | 23.9 | | A | 16.9 | | B | 19.8 | | A | 19.4 | | A | 17.1 | | B | 21.7 | | C | 23.7 | Output: | ssb | ssw | |---|---| | 93.8517 | 53.4550 | The group means are 16.525 (A), 19.9 (B) and 23.375 (C), and the grand mean is 239.2 / 12 = 19.93333. The squared gaps between each group mean and the grand mean are 11.61674, 0.00111 and 11.84507. Every group has 4 rows, so SSB is 4 times their sum, 93.85168, which rounds to 93.8517. SSW adds the squared distance of each value from its own group mean: 23.3675 for A, 20.94 for B and 9.1475 for C, a total of 53.455. ### Constraints and Clarifications - Neither column contains NULL, and the table has at least one row. - Identical rows are separate subjects. Each one counts toward $n_g$, both means and both sums. - 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. - A group with a single row contributes 0 to SSW. - Round only the two final sums, not the means used to build them. ```hint Sum over rows SSB is defined as a sum over groups, but it can also be written as a sum over rows: give every row its group's squared gap from the grand mean, and a group of $n_g$ rows adds that term $n_g$ times. ```

Overview: Write a PostgreSQL query that computes the between-group and within-group sums of squares of a one-way ANOVA from a table of grouped scores. It tests grouped aggregates or window averages, correct use of the grand mean with unequal group sizes, and exact numeric rounding.

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'. Compute the between-group sum of squares (SSB) and the within-group sum of squares (SSW):

SSB=∑gng(xˉg−xˉ)2SSB = \sum_{g} n_g \left(\bar{x}_g - \bar{x}\right)^2

SSW=∑g∑i∈g(xi−xˉg)2SSW = \sum_{g} \sum_{i \in g} \left(x_i - \bar{x}_g\right)^2

Here ngn_g and xˉg\bar{x}_g are the row count and mean value of group gg, xix_i is one row's value, and xˉ\bar{x} is the grand mean, the mean of value over all rows. Return one row with ssb and ssw, each rounded 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

One row with exactly these columns, in this order:

ColumnTypeMeaning
ssbNUMERICBetween-group sum of squares, rounded to 4 decimal places
sswNUMERICWithin-group sum of squares, rounded to 4 decimal places

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

Example

Input scores:

grpvalue
A12.7
B21.9
C20.9
C25
B16.2
C23.9
A16.9
B19.8
A19.4
A17.1
B21.7
C23.7

Output:

ssbssw
93.851753.4550

The group means are 16.525 (A), 19.9 (B) and 23.375 (C), and the grand mean is 239.2 / 12 = 19.93333. The squared gaps between each group mean and the grand mean are 11.61674, 0.00111 and 11.84507. Every group has 4 rows, so SSB is 4 times their sum, 93.85168, which rounds to 93.8517. SSW adds the squared distance of each value from its own group mean: 23.3675 for A, 20.94 for B and 9.1475 for C, a total of 53.455.

Constraints and Clarifications

  • Neither column contains NULL, and the table has at least one row.
  • Identical rows are separate subjects. Each one counts toward ngn_g , both means and both sums.
  • 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.
  • A group with a single row contributes 0 to SSW.
  • Round only the two final sums, not the means used to build them.