Compute a paired t-test statistic and its degrees of freedom in SQL
Quick Overview
Write a SQL query that turns before and after measurements for each subject into a paired t statistic and its degrees of freedom. It tests building per-subject differences, picking the right standard deviation aggregate, and rounding numeric results correctly in PostgreSQL.
Compute a paired t-test statistic and its degrees of freedom in SQL
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Table `measurements` has one row per subject with `subject_id`, `before_value` and `after_value`. With d = `after_value` - `before_value`, compute
$$t = \frac{\bar{d}}{s_d / \sqrt{n}}$$
where $\bar{d}$ is the mean of d, $s_d$ is the sample standard deviation of d and n is the number of pairs. Return one row: `t_statistic` (rounded to 4 decimals) and `df` (n - 1).
```hint Work on one column
A paired test needs only one number per subject. Build that column first, and check which standard deviation function divides by n - 1.
```
### Input Table(s)
Table `measurements`:
| Column | Type | Meaning |
|---|---|---|
| `subject_id` | INT | Subject identifier |
| `before_value` | DECIMAL(10,4) | Measurement before |
| `after_value` | DECIMAL(10,4) | Measurement after |
Primary key: `subject_id`. Each row is one before/after pair.
### Output Contract
One row with these columns, in this order:
| Column | Type | Meaning |
|---|---|---|
| `t_statistic` | NUMERIC | mean(d) / (sd(d) / sqrt(n)), rounded to 4 decimal places |
| `df` | INTEGER | n - 1 |
The result always has exactly one row, so no ordering is needed. d is after minus before, so a positive statistic means the values went up.
### Example
Input `measurements`:
| subject_id | before_value | after_value |
|---|---|---|
| 3514 | 48.7 | 56.8 |
| 8318 | 49.7 | 64.6 |
| 3465 | 54.6 | 63.6 |
| 6564 | 51.6 | 55 |
| 3614 | 46.7 | 51.8 |
| 517 | 43 | 58.4 |
| 4375 | 56 | 49.6 |
| 8101 | 47.4 | 62.1 |
Output:
| t_statistic | df |
|---|---|
| 3.0598 | 7 |
The eight differences are 8.1, 14.9, 9.0, 3.4, 5.1, 15.4, -6.4 and 14.7. Their mean is 8.025 and their sample standard deviation is about 7.4181, so the standard error is about 7.4181 / sqrt(8) = 2.6227 and t is about 3.0598, with 7 degrees of freedom.
### Constraints and Clarifications
- `subject_id` is unique, so there are no duplicate pairs to remove.
- Both values are filled in on every row. If a row ever had a NULL in either column, that pair drops out, so n counts only complete pairs.
- The table has at least 2 rows and the differences are not all equal, so the sample standard deviation is defined and positive.
- Round only the final statistic, to 4 decimal places.
Overview: Write a SQL query that turns before and after measurements for each subject into a paired t statistic and its degrees of freedom. It tests building per-subject differences, picking the right standard deviation aggregate, and rounding numeric results correctly in PostgreSQL.
Table measurements has one row per subject with subject_id, before_value and after_value. With d = after_value - before_value, compute
t=sd/ndˉ
where dˉ is the mean of d, sd is the sample standard deviation of d and n is the number of pairs. Return one row: t_statistic (rounded to 4 decimals) and df (n - 1).
Input Table(s)
Table measurements:
Column
Type
Meaning
subject_id
INT
Subject identifier
before_value
DECIMAL(10,4)
Measurement before
after_value
DECIMAL(10,4)
Measurement after
Primary key: subject_id. Each row is one before/after pair.
Output Contract
One row with these columns, in this order:
Column
Type
Meaning
t_statistic
NUMERIC
mean(d) / (sd(d) / sqrt(n)), rounded to 4 decimal places
df
INTEGER
n - 1
The result always has exactly one row, so no ordering is needed. d is after minus before, so a positive statistic means the values went up.
Example
Input measurements:
subject_id
before_value
after_value
3514
48.7
56.8
8318
49.7
64.6
3465
54.6
63.6
6564
51.6
55
3614
46.7
51.8
517
43
58.4
4375
56
49.6
8101
47.4
62.1
Output:
t_statistic
df
3.0598
7
The eight differences are 8.1, 14.9, 9.0, 3.4, 5.1, 15.4, -6.4 and 14.7. Their mean is 8.025 and their sample standard deviation is about 7.4181, so the standard error is about 7.4181 / sqrt(8) = 2.6227 and t is about 3.0598, with 7 degrees of freedom.
Constraints and Clarifications
subject_id
is unique, so there are no duplicate pairs to remove.
Both values are filled in on every row. If a row ever had a NULL in either column, that pair drops out, so n counts only complete pairs.
The table has at least 2 rows and the differences are not all equal, so the sample standard deviation is defined and positive.
Round only the final statistic, to 4 decimal places.