Bracket a two-sample t-test p-value in SQL using a table of critical values
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, `t_critical`, holds the two-tailed critical value of Student's t for each `df` at `alpha` 0.100, 0.050, 0.010 and 0.001. Compute the pooled-variance two-sample t-statistic and its tightest two-tailed p-value bracket from the table. Return one row with `t_statistic`, `df` and `p_value_lt`.
Control is group 1 and treatment is group 2, and `df = n1 + n2 - 2`. The bracket `p_value_lt` is the smallest `alpha` for that `df` whose `critical_value` is at most $|t|$. Clearing the critical value at a level means the two-tailed p-value is at most that level, so the smallest cleared level is the tightest upper bound the table supports. When $|t|$ clears none of the levels, return 0.200, a fixed fallback that means the result is not significant even at 0.10. Round `t_statistic` to 4 decimals.
```hint An empty set of levels
Think of the levels whose threshold $|t|$ clears for this `df` as a set. The bracket is one end of that set, and the set can be empty.
```
### 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 |
`t_critical` 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: 0.100, 0.050, 0.010 or 0.001 |
| `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 |
| `p_value_lt` | DECIMAL | Smallest `alpha` for this `df` whose `critical_value` is at most the absolute unrounded statistic, or 0.200 when there is none |
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$). `p_value_lt` is always one of 0.001, 0.010, 0.050, 0.100 or 0.200 and is compared as a number, so 0.01 and 0.010 are the same answer. The result has a single row, so it needs no ordering.
### Example
`measurements`
| group_name | value |
|---|---|
| control | 6.4 |
| treatment | 6.8 |
| treatment | 5.8 |
| treatment | 5.9 |
| control | 4.6 |
| control | 4.5 |
| treatment | 6.6 |
| control | 4.6 |
| control | 5 |
| control | 5.6 |
| treatment | 6.1 |
| treatment | 6.2 |
`t_critical`
| df | alpha | critical_value |
|---|---|---|
| 10 | 0.1 | 1.812 |
| 10 | 0.05 | 2.228 |
| 10 | 0.01 | 3.169 |
| 10 | 0.001 | 4.587 |
| 30 | 0.1 | 1.697 |
| 30 | 0.05 | 2.042 |
| 30 | 0.01 | 2.75 |
| 30 | 0.001 | 3.646 |
Expected result
| t_statistic | df | p_value_lt |
|---|---|---|
| -3.2318 | 10 | 0.01 |
Control has mean 5.1167 and sample variance 0.5617; treatment has mean 6.2333 and sample variance 0.1547. The pooled variance is 0.3582, the standard error is about 0.3455, and $t = -1.1167 / 0.3455 \approx -3.2318$ with 10 degrees of freedom. For `df = 10`, the absolute value 3.2318 clears 1.812, 2.228 and 3.169 but not 4.587, so the tightest bracket is 0.010. The `df = 30` rows are ignored.
### Constraints and Clarifications
- Each group has at least two rows and the pooled variance is positive, so the statistic is always defined.
- `t_critical` has all four levels for the computed `df`. Rows for other `df` values must not affect the result.
- Within one `df`, critical values grow as `alpha` shrinks, so the cleared levels are always the largest ones.
- A statistic whose absolute value exactly equals a critical value clears that level.
- Repeated values are separate observations. 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.
Overview: Compute a pooled two-sample t-statistic and its degrees of freedom in SQL, then report the tightest two-tailed p-value bracket from a table of critical values. It tests conditional aggregation, a scalar subquery with a fallback value, and the case where the statistic clears no threshold.