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.

Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

The paired table holds nn 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 rr with the raw-sum formula:

r=n∑xy−∑x∑yn∑x2−(∑x)2 n∑y2−(∑y)2r = \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=rn−2/1−r2t = r\sqrt{n - 2} / \sqrt{1 - r^2} with df =n−2= n - 2, and compare ∣t∣\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 ∣t∣\lvert t \rvert is greater than the critical value and 0 otherwise.

Input Table(s)

paired

ColumnTypeMeaning
xDECIMAL(10,4)First measurement of the pair
yDECIMAL(10,4)Second measurement of the pair

The table has no key column. Each row is one observation pair.

critical_values

ColumnTypeMeaning
dfINTDegrees of freedom
alphaDECIMAL(4,3)Two-tailed significance level
critical_valueDECIMAL(8,4)Two-tailed t critical value for that df and alpha

Primary key: (df, alpha).

Output Contract

ColumnTypeMeaning
correlationNUMERICPearson rr, rounded to 4 decimal places
t_statisticNUMERICtt, rounded to 4 decimal places
dfINTEGERn−2n - 2
significantINTEGER1 if the unrounded ∣t∣\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:

xy
19.426.5
7.911.9
4357.9
28.426.6
1.16.5
10.312.9
5.76.7
3.714.7
20.620.1
9.310.1

Input critical_values:

dfalphacritical_value
80.052.306
90.052.2622
100.052.2281
110.052.201
120.052.1788
130.052.1604
140.052.1448
150.052.1314
160.052.1199
170.052.1098
180.052.1009
190.052.093
200.052.086
210.052.0796
220.052.0739
230.052.0687
240.052.0639
250.052.0595
260.052.0555
270.052.0518
280.052.0484

Output:

correlationt_statisticdfsignificant
0.94338.040381

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≥3n \ge 3 .
  • Neither column is constant, and the points do not lie exactly on a line, so both square roots in the denominator of rr are positive and ∣r∣<1\lvert r \rvert < 1 .
  • critical_values has a row for the computed df at alpha = 0.05.
  • Compute rr 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 tt from the unrounded rr , and make the decision with the unrounded tt .