Test a Pearson Correlation for Significance with a t-Statistic in SQL
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
The `paired` table holds $n$ paired observations `x` and `y`. The `critical_values` table holds two-tailed t critical values keyed by `df` and `alpha`. Compute the Pearson correlation $r$ with the raw-sum formula:
$$r = \frac{n\sum xy - \sum x \sum y}{\sqrt{n\sum x^2 - \left(\sum x\right)^2}\,\sqrt{n\sum y^2 - \left(\sum y\right)^2}}$$
Then compute its significance t-statistic with $n - 2$ degrees of freedom:
$$t = \frac{r\sqrt{n - 2}}{\sqrt{1 - r^2}}$$
Return one row with:
- `t_statistic`, rounded to 4 decimals.
- `df`, equal to $n - 2$.
- `critical_value`, looked up in `critical_values` for that `df` at `alpha` = 0.05.
- `significant`, equal to 1 when $|t|$ is greater than `critical_value` and 0 otherwise.
```hint Keep an eye on types
In PostgreSQL, `ROUND(value, 4)` exists only for `numeric`. Check whether any step, such as a square root of an integer, turns your intermediate values into `double precision`.
```
### Input Table(s)
`paired`
| Column | Type | Meaning |
|---|---|---|
| x | DECIMAL(10,4) | First measurement of the pair |
| y | DECIMAL(10,4) | Second measurement of the pair |
The table has no key column. Each row is one observation pair.
`critical_values`
| Column | Type | Meaning |
|---|---|---|
| df | INT | Degrees of freedom |
| alpha | DECIMAL(4,3) | Two-tailed significance level |
| critical_value | DECIMAL(8,4) | Two-tailed t critical value for that `df` and `alpha` |
Primary key: (`df`, `alpha`).
### Output Contract
| Column | Type | Meaning |
|---|---|---|
| t_statistic | NUMERIC | $t$, rounded to 4 decimal places |
| df | INTEGER | $n - 2$ |
| critical_value | DECIMAL(8,4) | Critical value for `df` at `alpha` = 0.05, as stored |
| significant | INTEGER | 1 if the unrounded $\lvert t \rvert$ exceeds `critical_value`, else 0 |
The result is exactly one row, so no ordering is needed.
### Example
Input `paired`:
| x | y |
|---|---|
| 26.2 | 36.7 |
| 44.7 | 67.9 |
| 43.3 | 70.9 |
| 19.6 | 38.2 |
| 8.4 | 24.6 |
| 9.5 | 11.7 |
| 7.7 | 8.9 |
| 49.5 | 79.1 |
| 42.1 | 55.6 |
| 39.1 | 60 |
| 43.6 | 57.9 |
Input `critical_values`:
| df | alpha | critical_value |
|---|---|---|
| 8 | 0.05 | 2.306 |
| 9 | 0.05 | 2.2622 |
| 10 | 0.05 | 2.2281 |
| 11 | 0.05 | 2.201 |
| 12 | 0.05 | 2.1788 |
| 13 | 0.05 | 2.1604 |
| 14 | 0.05 | 2.1448 |
| 15 | 0.05 | 2.1314 |
| 16 | 0.05 | 2.1199 |
| 17 | 0.05 | 2.1098 |
| 18 | 0.05 | 2.1009 |
| 19 | 0.05 | 2.093 |
| 20 | 0.05 | 2.086 |
| 21 | 0.05 | 2.0796 |
| 22 | 0.05 | 2.0739 |
| 23 | 0.05 | 2.0687 |
| 24 | 0.05 | 2.0639 |
| 25 | 0.05 | 2.0595 |
| 26 | 0.05 | 2.0555 |
| 27 | 0.05 | 2.0518 |
| 28 | 0.05 | 2.0484 |
Output:
| t_statistic | df | critical_value | significant |
|---|---|---|---|
| 11.2113 | 9 | 2.2622 | 1 |
There are 11 pairs, so df is 9. The correlation is about 0.966, which gives a t far above the df 9 critical value of 2.2622.
### Constraints and Clarifications
- Neither `x` nor `y` is NULL, and $n \ge 3$.
- Neither column is constant, and the points do not lie exactly on a line, so both square roots in the denominator of $r$ are positive and $\lvert r \rvert < 1$.
- `critical_values` has a row for the computed `df` at `alpha` = 0.05.
- Compute $r$ from the count and the sums; do not call `CORR()`.
- Identical (`x`, `y`) rows are separate observations and all count.
- Round only `t_statistic` in the output. The comparison for `significant` uses the unrounded value.
Overview: Write a PostgreSQL query that computes a Pearson correlation from raw sums, converts it to a t-statistic with n minus 2 degrees of freedom, and flags significance against a table of two-tailed critical values. It tests the correlation t-test, numeric types in SQL and an exact lookup join.