Pooled Variance and Pooled Standard Error of Two Samples in SQL
Quick Overview
Write a SQL query that computes the pooled variance of two independent samples and the pooled standard error of their difference in means. It tests conditional aggregation, the sample variance, degrees-of-freedom weighting and numeric division in PostgreSQL.
Pooled Variance and Pooled Standard Error of Two Samples in SQL
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Table `measurements` holds two independent samples, labeled `'control'` (group 1) and `'treatment'` (group 2). Compute the pooled variance and the pooled standard error of the difference in means:
$$
s_p^2 = \frac{(n_1 - 1)s_1^2 + (n_2 - 1)s_2^2}{n_1 + n_2 - 2}, \qquad SE = \sqrt{s_p^2 \left(\frac{1}{n_1} + \frac{1}{n_2}\right)}
$$
Here $n_i$ is the number of values in group $i$ and $s_i^2$ is its sample variance. Return one row with `pooled_variance` and `pooled_se`, both rounded to 4 decimals.
```hint Division pitfall
Make sure `1 / n` comes out as a fraction. With two integer operands, PostgreSQL truncates it to zero.
```
### Input Table(s)
`measurements`
| Column | Type | Meaning |
|---|---|---|
| group_name | VARCHAR(20) | `'control'` or `'treatment'` |
| value | DECIMAL(10,4) | The continuous measurement |
There is no primary key. Each row is one measurement, and two rows with the same value are two separate observations.
### Output Contract
Exactly one row with these columns, in this order:
| Column | Meaning |
|---|---|
| pooled_variance | $s_p^2$, rounded to 4 decimals |
| pooled_se | $SE$, rounded to 4 decimals |
Compute `pooled_se` from the unrounded pooled variance and round only the output columns. The result is a single row, so no ordering applies.
### Example
`measurements`
| group_name | value |
|---|---|
| control | 4.8 |
| treatment | 7.4 |
| control | 6.8 |
| treatment | 7.5 |
| control | 7 |
| treatment | 6.5 |
| treatment | 8.3 |
| control | 4.7 |
Expected result:
| pooled_variance | pooled_se |
|---|---|
| 1.0458 | 0.7231 |
Control has 4 values with mean 5.825 and squared deviations summing to 4.6475, so its sample variance is 1.5492. Treatment has 4 values with mean 7.425 and squared deviations summing to 1.6275, so its sample variance is 0.5425. The pooled variance is ${(4.6475 + 1.6275) / 6 \approx 1.0458}$, and the standard error is ${\sqrt{1.0458 \cdot (1/4 + 1/4)} \approx 0.7231}$.
### Constraints and Clarifications
- Each group has at least 2 non-NULL values, so both sample variances and the denominator $n_1 + n_2 - 2$ are defined.
- A row whose `value` is NULL is ignored by every per-group statistic, both the count and the variance.
- Duplicate rows are separate measurements. Do not deduplicate them.
- `group_name` holds only the two lowercase labels `'control'` and `'treatment'`.
- The variances are sample variances, with $n - 1$ in the denominator.
- The group sizes may differ, and the query must weight each variance by its own degrees of freedom.
Overview: Write a SQL query that computes the pooled variance of two independent samples and the pooled standard error of their difference in means. It tests conditional aggregation, the sample variance, degrees-of-freedom weighting and numeric division in PostgreSQL.
Table measurements holds two independent samples, labeled 'control' (group 1) and 'treatment' (group 2). Compute the pooled variance and the pooled standard error of the difference in means:
Here ni is the number of values in group i and si2 is its sample variance. Return one row with pooled_variance and pooled_se, both rounded to 4 decimals.
Input Table(s)
measurements
Column
Type
Meaning
group_name
VARCHAR(20)
'control' or 'treatment'
value
DECIMAL(10,4)
The continuous measurement
There is no primary key. Each row is one measurement, and two rows with the same value are two separate observations.
Output Contract
Exactly one row with these columns, in this order:
Column
Meaning
pooled_variance
sp2, rounded to 4 decimals
pooled_se
SE, rounded to 4 decimals
Compute pooled_se from the unrounded pooled variance and round only the output columns. The result is a single row, so no ordering applies.
Example
measurements
group_name
value
control
4.8
treatment
7.4
control
6.8
treatment
7.5
control
7
treatment
6.5
treatment
8.3
control
4.7
Expected result:
pooled_variance
pooled_se
1.0458
0.7231
Control has 4 values with mean 5.825 and squared deviations summing to 4.6475, so its sample variance is 1.5492. Treatment has 4 values with mean 7.425 and squared deviations summing to 1.6275, so its sample variance is 0.5425. The pooled variance is (4.6475+1.6275)/6≈1.0458, and the standard error is 1.0458⋅(1/4+1/4)≈0.7231.
Constraints and Clarifications
Each group has at least 2 non-NULL values, so both sample variances and the denominator
n1+n2−2
are defined.
A row whose
value
is NULL is ignored by every per-group statistic, both the count and the variance.
Duplicate rows are separate measurements. Do not deduplicate them.
group_name
holds only the two lowercase labels
'control'
and
'treatment'
.
The variances are sample variances, with
n−1
in the denominator.
The group sizes may differ, and the query must weight each variance by its own degrees of freedom.