Confidence Interval for a Difference in Means in SQL
Quick Overview
Write a SQL query that builds a confidence interval for the difference between control and treatment means, using a pooled variance and a critical value stored in a separate table. It tests conditional aggregation, the sample variance, numeric types and rounding in PostgreSQL.
Confidence Interval for a Difference in Means in SQL
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Table `measurements` holds one continuous measurement per row, labeled `'control'` or `'treatment'`. Table `t_critical` holds a single critical value. Build a confidence interval for the difference in means, with `'control'` as group 1 and `'treatment'` as group 2, using the pooled two-sample formula:
$$
(\bar{x}_1 - \bar{x}_2) \pm t^{*} \cdot SE
$$
$$
SE = \sqrt{s_p^2 \left(\frac{1}{n_1} + \frac{1}{n_2}\right)}, \qquad s_p^2 = \frac{(n_1 - 1)s_1^2 + (n_2 - 1)s_2^2}{n_1 + n_2 - 2}
$$
Here $t^{*}$ is `critical_value`, $n_i$ is the number of values in group $i$, $\bar{x}_i$ is its mean and $s_i^2$ is its sample variance. Return one row with `mean_diff`, `ci_lower` and `ci_upper`, each rounded to 4 decimals.
```hint Numeric pitfalls
Get every per-group count, mean and variance from a single pass over `measurements`, and keep the arithmetic in numeric types so that `1 / n` does not truncate 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.
`t_critical`
| Column | Type | Meaning |
|---|---|---|
| critical_value | DECIMAL(6,4) | The critical value that multiplies the standard error |
The table has exactly one row and no key.
### Output Contract
Exactly one row with these columns, in this order:
| Column | Meaning |
|---|---|
| mean_diff | Control mean minus treatment mean |
| ci_lower | `mean_diff` minus `critical_value` times SE |
| ci_upper | `mean_diff` plus `critical_value` times SE |
Round each column to 4 decimal places, and round only the final values: compute both bounds from the unrounded difference and standard error. The result is a single row, so no ordering applies.
### Example
`measurements`
| group_name | value |
|---|---|
| treatment | 6 |
| control | 6.3 |
| control | 4.7 |
| control | 6.5 |
| treatment | 7.4 |
| treatment | 7.8 |
| control | 4.7 |
| control | 5.5 |
| treatment | 6.5 |
| treatment | 6.8 |
| control | 5.8 |
| treatment | 7.7 |
`t_critical`
| critical_value |
|---|
| 2.048 |
Expected result:
| mean_diff | ci_lower | ci_upper |
|---|---|---|
| -1.4500 | -2.3302 | -0.5698 |
Control has 6 values with mean 5.5833 and sample variance 0.5937. Treatment has 6 values with mean 7.0333 and sample variance 0.5147. The pooled variance is 0.5542, the standard error is ${\sqrt{0.5542 \cdot (1/6 + 1/6)} \approx 0.4298}$, and the margin is ${2.048 \cdot 0.4298 \approx 0.8802}$, which puts the interval at -1.45 plus or minus 0.8802.
### Constraints and Clarifications
- Each group has at least 2 non-NULL values, so both sample variances and the pooled denominator are defined.
- A row whose `value` is NULL is ignored by every per-group statistic: the count, the mean and the variance.
- Duplicate rows are separate measurements. Do not deduplicate them.
- `group_name` holds only the two lowercase labels `'control'` and `'treatment'`.
- `t_critical` has exactly one row.
- The variances are sample variances, with $n - 1$ in the denominator.
Overview: Write a SQL query that builds a confidence interval for the difference between control and treatment means, using a pooled variance and a critical value stored in a separate table. It tests conditional aggregation, the sample variance, numeric types and rounding in PostgreSQL.
Table measurements holds one continuous measurement per row, labeled 'control' or 'treatment'. Table t_critical holds a single critical value. Build a confidence interval for the difference in means, with 'control' as group 1 and 'treatment' as group 2, using the pooled two-sample formula:
Here t∗ is critical_value, ni is the number of values in group i, xˉi is its mean and si2 is its sample variance. Return one row with mean_diff, ci_lower and ci_upper, each 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.
t_critical
Column
Type
Meaning
critical_value
DECIMAL(6,4)
The critical value that multiplies the standard error
The table has exactly one row and no key.
Output Contract
Exactly one row with these columns, in this order:
Column
Meaning
mean_diff
Control mean minus treatment mean
ci_lower
mean_diff minus critical_value times SE
ci_upper
mean_diff plus critical_value times SE
Round each column to 4 decimal places, and round only the final values: compute both bounds from the unrounded difference and standard error. The result is a single row, so no ordering applies.
Example
measurements
group_name
value
treatment
6
control
6.3
control
4.7
control
6.5
treatment
7.4
treatment
7.8
control
4.7
control
5.5
treatment
6.5
treatment
6.8
control
5.8
treatment
7.7
t_critical
critical_value
2.048
Expected result:
mean_diff
ci_lower
ci_upper
-1.4500
-2.3302
-0.5698
Control has 6 values with mean 5.5833 and sample variance 0.5937. Treatment has 6 values with mean 7.0333 and sample variance 0.5147. The pooled variance is 0.5542, the standard error is 0.5542⋅(1/6+1/6)≈0.4298, and the margin is 2.048⋅0.4298≈0.8802, which puts the interval at -1.45 plus or minus 0.8802.
Constraints and Clarifications
Each group has at least 2 non-NULL values, so both sample variances and the pooled denominator are defined.
A row whose
value
is NULL is ignored by every per-group statistic: the count, the mean and the variance.
Duplicate rows are separate measurements. Do not deduplicate them.
group_name
holds only the two lowercase labels
'control'
and
'treatment'
.
t_critical
has exactly one row.
The variances are sample variances, with
n−1
in the denominator.