Decide Whether Two Variables Are Significantly Correlated Using a t-Test in SQL
Quick Overview
Write a PostgreSQL query that reports a Pearson correlation computed from raw sums, its t-statistic and degrees of freedom, and a 0 or 1 decision on whether the correlation is significant at the 5% level. It tests the two-tailed correlation t-test and exact lookups against a critical value table.
Decide Whether Two Variables Are Significantly Correlated Using a t-Test 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`, and `critical_values` supplies two-tailed t critical values keyed by `df` and `alpha`. Decide whether `x` and `y` are significantly correlated at `alpha` = 0.05.
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 $t = r\sqrt{n - 2} / \sqrt{1 - r^2}$ with `df` $= n - 2$, and compare $\lvert t \rvert$ to the `critical_value` for that `df` at `alpha` = 0.05. Return one row with `correlation` and `t_statistic` (each rounded to 4 decimals), `df`, and `significant`, which is 1 when $\lvert t \rvert$ is greater than the critical value and 0 otherwise.
```hint Mind the sign
$r$ and $t$ can be negative. Make sure a strong negative correlation can still be flagged as significant.
```
### 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 |
|---|---|---|
| correlation | NUMERIC | Pearson $r$, rounded to 4 decimal places |
| t_statistic | NUMERIC | $t$, rounded to 4 decimal places |
| df | INTEGER | $n - 2$ |
| significant | INTEGER | 1 if the unrounded $\lvert t \rvert$ exceeds the critical value for `df` at `alpha` = 0.05, else 0 |
The result is exactly one row, so no ordering is needed.
### Example
Input `paired`:
| x | y |
|---|---|
| 19.4 | 26.5 |
| 7.9 | 11.9 |
| 43 | 57.9 |
| 28.4 | 26.6 |
| 1.1 | 6.5 |
| 10.3 | 12.9 |
| 5.7 | 6.7 |
| 3.7 | 14.7 |
| 20.6 | 20.1 |
| 9.3 | 10.1 |
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:
| correlation | t_statistic | df | significant |
|---|---|---|---|
| 0.9433 | 8.0403 | 8 | 1 |
With 10 pairs, df is 8 and the critical value is 2.306. A correlation of 0.9433 gives a t of about 8.04, well past it.
### 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 `correlation` and `t_statistic` only in the output. Compute $t$ from the unrounded $r$, and make the decision with the unrounded $t$.
Overview: Write a PostgreSQL query that reports a Pearson correlation computed from raw sums, its t-statistic and degrees of freedom, and a 0 or 1 decision on whether the correlation is significant at the 5% level. It tests the two-tailed correlation t-test and exact lookups against a critical value table.
The paired table holds n paired observations x and y, and critical_values supplies two-tailed t critical values keyed by df and alpha. Decide whether x and y are significantly correlated at alpha = 0.05.
Compute the Pearson correlation r with the raw-sum formula:
r=n∑x2−(∑x)2n∑y2−(∑y)2n∑xy−∑x∑y
Then compute t=rn−2/1−r2 with df=n−2, and compare ∣t∣ to the critical_value for that df at alpha = 0.05. Return one row with correlation and t_statistic (each rounded to 4 decimals), df, and significant, which is 1 when ∣t∣ is greater than the critical value and 0 otherwise.
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
correlation
NUMERIC
Pearson r, rounded to 4 decimal places
t_statistic
NUMERIC
t, rounded to 4 decimal places
df
INTEGER
n−2
significant
INTEGER
1 if the unrounded ∣t∣ exceeds the critical value for df at alpha = 0.05, else 0
The result is exactly one row, so no ordering is needed.
Example
Input paired:
x
y
19.4
26.5
7.9
11.9
43
57.9
28.4
26.6
1.1
6.5
10.3
12.9
5.7
6.7
3.7
14.7
20.6
20.1
9.3
10.1
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:
correlation
t_statistic
df
significant
0.9433
8.0403
8
1
With 10 pairs, df is 8 and the critical value is 2.306. A correlation of 0.9433 gives a t of about 8.04, well past it.
Constraints and Clarifications
Neither
x
nor
y
is NULL, and
n≥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
∣r∣<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
correlation
and
t_statistic
only in the output. Compute
t
from the unrounded
r
, and make the decision with the unrounded
t
.