Compute a pooled two-sample t-statistic in SQL and flag significance at alpha 0.05
Quick Overview
Write a SQL query that computes a pooled-variance two-sample t-statistic for control and treatment groups, then looks up the critical value at alpha 0.05 to flag significance. It tests conditional aggregation, the sample variance and the use of the absolute statistic in a two-tailed test.
Compute a pooled two-sample t-statistic in SQL and flag significance at alpha 0.05
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
An experiment stores one row per observation in `measurements`, where `group_name` is `'control'` or `'treatment'`. A lookup table, `critical_values`, holds the two-tailed critical value of Student's t for each `df` and `alpha`. Compute the pooled-variance two-sample t-statistic comparing control with treatment, look up the critical value for its degrees of freedom at `alpha = 0.05`, and flag whether the difference is significant. Return one row with `t_statistic`, `df` and `significant`.
Control is group 1 and treatment is group 2, `df = n1 + n2 - 2`, and `significant` is 1 when `|t| >= critical_value`, else 0. Round `t_statistic` to 4 decimals.
```hint One scan of the table
An aggregate wrapped around a CASE expression lets a single pass over `measurements` return the count, mean and sample variance of each group side by side.
```
### Input Table(s)
`measurements` has one row per observation. It has no key, and identical rows are separate observations.
| Column | Type | Meaning |
|---|---|---|
| `group_name` | VARCHAR(20) | `'control'` or `'treatment'` |
| `value` | DECIMAL(10,4) | The measured value |
`critical_values` has one row per `(df, alpha)` pair, which is its key.
| Column | Type | Meaning |
|---|---|---|
| `df` | INT | Degrees of freedom |
| `alpha` | DECIMAL(4,3) | Significance level |
| `critical_value` | DECIMAL(8,4) | Two-tailed critical value of t at that `df` and `alpha` |
### Output Contract
Exactly one row with these columns, in this order:
| Column | Type | Definition |
|---|---|---|
| `t_statistic` | NUMERIC | Pooled two-sample t-statistic, control minus treatment, rounded to 4 decimals |
| `df` | INT | n1 + n2 - 2 |
| `significant` | INT | 1 when the absolute value of the unrounded statistic is at least the `critical_value` for `df` at `alpha = 0.05`, else 0 |
The statistic is
$$
t = \frac{\bar{x}_1 - \bar{x}_2}{\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}
$$
where $s_1^2$ and $s_2^2$ are the sample variances (divisor $n_i - 1$). Group 1 is control, so `t_statistic` is negative when the treatment mean is higher. The result has a single row, so it needs no ordering.
### Example
`measurements`
| group_name | value |
|---|---|
| treatment | 6.8 |
| treatment | 7.1 |
| control | 4.2 |
| treatment | 7.9 |
| control | 4.8 |
| treatment | 7 |
| control | 4.2 |
| control | 5.7 |
| control | 4.1 |
| control | 4.9 |
| treatment | 6.7 |
| treatment | 7 |
`critical_values`
| df | alpha | critical_value |
|---|---|---|
| 10 | 0.05 | 2.228 |
| 40 | 0.05 | 2.021 |
Expected result
| t_statistic | df | significant |
|---|---|---|
| -7.9602 | 10 | 1 |
Control has six values with mean 4.65 and sample variance 0.379. Treatment has six values with mean 7.0833 and sample variance 0.1817. The pooled variance is 0.2803, the standard error is $\sqrt{0.2803 \cdot (1/6 + 1/6)} \approx 0.3057$, and $t = -2.4333 / 0.3057 \approx -7.9602$ with 10 degrees of freedom. Its absolute value exceeds 2.228, so `significant` is 1. The `df = 40` row plays no part.
### Constraints and Clarifications
- Each group has at least two rows and the pooled variance is positive, so the statistic is always defined.
- `critical_values` contains a row for the computed `df` at `alpha = 0.05`. Rows for other `df` values or other levels must not change the result.
- Repeated values are separate observations (control has 4.2 twice and treatment has 7 twice in the example). Do not deduplicate.
- `value` is not NULL in the test data. If a NULL did appear, it should be left out of that group's count, mean and variance alike.
- A statistic whose absolute value exactly equals the critical value counts as significant.
Overview: Write a SQL query that computes a pooled-variance two-sample t-statistic for control and treatment groups, then looks up the critical value at alpha 0.05 to flag significance. It tests conditional aggregation, the sample variance and the use of the absolute statistic in a two-tailed test.
An experiment stores one row per observation in measurements, where group_name is 'control' or 'treatment'. A lookup table, critical_values, holds the two-tailed critical value of Student's t for each df and alpha. Compute the pooled-variance two-sample t-statistic comparing control with treatment, look up the critical value for its degrees of freedom at alpha = 0.05, and flag whether the difference is significant. Return one row with t_statistic, df and significant.
Control is group 1 and treatment is group 2, df = n1 + n2 - 2, and significant is 1 when |t| >= critical_value, else 0. Round t_statistic to 4 decimals.
Input Table(s)
measurements has one row per observation. It has no key, and identical rows are separate observations.
Column
Type
Meaning
group_name
VARCHAR(20)
'control' or 'treatment'
value
DECIMAL(10,4)
The measured value
critical_values has one row per (df, alpha) pair, which is its key.
Column
Type
Meaning
df
INT
Degrees of freedom
alpha
DECIMAL(4,3)
Significance level
critical_value
DECIMAL(8,4)
Two-tailed critical value of t at that df and alpha
Output Contract
Exactly one row with these columns, in this order:
Column
Type
Definition
t_statistic
NUMERIC
Pooled two-sample t-statistic, control minus treatment, rounded to 4 decimals
df
INT
n1 + n2 - 2
significant
INT
1 when the absolute value of the unrounded statistic is at least the critical_value for df at alpha = 0.05, else 0
where s12 and s22 are the sample variances (divisor ni−1). Group 1 is control, so t_statistic is negative when the treatment mean is higher. The result has a single row, so it needs no ordering.
Example
measurements
group_name
value
treatment
6.8
treatment
7.1
control
4.2
treatment
7.9
control
4.8
treatment
7
control
4.2
control
5.7
control
4.1
control
4.9
treatment
6.7
treatment
7
critical_values
df
alpha
critical_value
10
0.05
2.228
40
0.05
2.021
Expected result
t_statistic
df
significant
-7.9602
10
1
Control has six values with mean 4.65 and sample variance 0.379. Treatment has six values with mean 7.0833 and sample variance 0.1817. The pooled variance is 0.2803, the standard error is 0.2803⋅(1/6+1/6)≈0.3057, and t=−2.4333/0.3057≈−7.9602 with 10 degrees of freedom. Its absolute value exceeds 2.228, so significant is 1. The df = 40 row plays no part.
Constraints and Clarifications
Each group has at least two rows and the pooled variance is positive, so the statistic is always defined.
critical_values
contains a row for the computed
df
at
alpha = 0.05
. Rows for other
df
values or other levels must not change the result.
Repeated values are separate observations (control has 4.2 twice and treatment has 7 twice in the example). Do not deduplicate.
value
is not NULL in the test data. If a NULL did appear, it should be left out of that group's count, mean and variance alike.
A statistic whose absolute value exactly equals the critical value counts as significant.