Test a Pearson Correlation for Significance with a t-Statistic in SQL

Quick 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.

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.

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

The paired table holds nn paired observations x and y. The critical_values table holds two-tailed t critical values keyed by df and alpha. 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 its significance t-statistic with n−2n - 2 degrees of freedom:

t=rn−21−r2t = \frac{r\sqrt{n - 2}}{\sqrt{1 - r^2}}

Return one row with:

  • t_statistic , rounded to 4 decimals.
  • df , equal to n−2n - 2 .
  • critical_value , looked up in critical_values for that df at alpha = 0.05.
  • significant , equal to 1 when ∣t∣|t| is greater than 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
t_statisticNUMERICtt, rounded to 4 decimal places
dfINTEGERn−2n - 2
critical_valueDECIMAL(8,4)Critical value for df at alpha = 0.05, as stored
significantINTEGER1 if the unrounded ∣t∣\lvert t \rvert exceeds critical_value, else 0

The result is exactly one row, so no ordering is needed.

Example

Input paired:

xy
26.236.7
44.767.9
43.370.9
19.638.2
8.424.6
9.511.7
7.78.9
49.579.1
42.155.6
39.160
43.657.9

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:

t_statisticdfcritical_valuesignificant
11.211392.26221

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≥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 only t_statistic in the output. The comparison for significant uses the unrounded value.