Compute eta-squared, the share of variance explained by group, in SQL
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
The `scores` table records a numeric `value` for each subject, and each subject belongs to a group `grp` (`'A'`, `'B'` or `'C'`). Compute `eta_squared` = SSB / SST, where SSB = SUM(n_g * (mean_g - grand_mean)^2) over the groups and SST = SUM((value - grand_mean)^2) over all rows. Here n_g and mean_g are the size and mean of group g, and grand_mean is the mean of every value in the table. Return one row with `eta_squared` rounded to 4 decimals.
```hint One mean, two sums
Both sums of squares measure distance from the same grand mean. SSB needs one row per group and SST needs one row per subject.
```
### Input Table(s)
Table `scores`:
| Column | Type | Meaning |
|---|---|---|
| grp | VARCHAR(5) | The subject's group: `'A'`, `'B'` or `'C'` |
| value | NUMERIC(10,4) | The subject's score |
There is no key. Each row is one subject, and two subjects can have the same group and value.
### Output Contract
- Exactly one row with one column, `eta_squared`.
- `eta_squared` is SSB divided by SST, rounded to 4 decimals.
- If SST is 0 (every value in the table is the same), `eta_squared` is NULL.
### Example
Input:
```text
scores
+-----+---------+
| grp | value |
+-----+---------+
| B | 17.6000 |
| B | 24.4000 |
| C | 27.6000 |
| C | 27.9000 |
| A | 16.6000 |
| A | 17.1000 |
| A | 17.8000 |
| B | 17.0000 |
| C | 28.2000 |
| A | 18.7000 |
| C | 26.5000 |
| B | 19.8000 |
+-----+---------+
```
Expected output:
```text
+-------------+
| eta_squared |
+-------------+
| 0.8539 |
+-------------+
```
The group means are 17.55 (A), 19.7 (B) and 27.55 (C), and the grand mean is 21.6. SSB = 4 * 16.4025 + 4 * 3.61 + 4 * 35.4025 = 221.66, SST = 259.6, and 221.66 / 259.6 rounds to 0.8539.
### Constraints and Clarifications
- `grp` and `value` are never NULL, and the table has at least one row.
- Groups can have different sizes. Each group's term in SSB is weighted by its own row count.
- The grand mean is the mean of all rows, not the mean of the three group means.
Overview: Write a SQL query that computes eta-squared, the share of total variance in a numeric score explained by group membership, from the between-group and total sums of squares. It tests size-weighted group terms, reusing one grand mean, and safe division when every value is the same.
The `scores` table records a numeric `value` for each subject, and each subject belongs to a group `grp` (`'A'`, `'B'` or `'C'`). Each row is one subject. The table has no key, and two subjects can have the same group and value; every row counts.
Compute `eta_squared` = SSB / SST, where:
- `grand_mean` is the mean of `value` over every row in the table. Groups can have different sizes, so this row mean of the whole table can differ from the average of the three group means, and the row mean is the one to use.
- For each group g in the table, `n_g` is its row count and `mean_g` is the mean of its values.
- `SSB = SUM(n_g * (mean_g - grand_mean)^2)` over the groups, so each group's term is weighted by its own row count.
- `SST = SUM((value - grand_mean)^2)` over all rows.
Return exactly one row with one column, `eta_squared`, equal to SSB divided by SST and rounded to 4 decimals. If SST is 0 (every value in the table is the same), `eta_squared` is NULL.
`grp` and `value` are never NULL, and the table has at least one row.
Tables
scores(grp VARCHAR(5), value NUMERIC(10,4))
Hints
- One mean, two sums. Both sums of squares measure distance from the same grand mean. SSB needs one row per group and SST needs one row per subject.
- When every value in the table is the same, SST is 0. The output contract asks for NULL in that case, so guard the division.