Compute R-squared in SQL by squaring the Pearson correlation of two columns
Quick Overview
Write a PostgreSQL query that computes the Pearson correlation of two paired numeric columns from raw sums, squares it to get R-squared, and rounds the result to four decimals. It tests single-pass aggregation, translating a regression statistic into SQL, and rounding only at the final step.
Compute R-squared in SQL by squaring the Pearson correlation of two columns
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
The table `paired` holds paired observations `x` and `y`, one pair per row. Compute the Pearson correlation r with the raw-sum formula
$$
r = \frac{n S_{xy} - S_x S_y}{\sqrt{n S_{xx} - S_x^2}\,\sqrt{n S_{yy} - S_y^2}}
$$
where $n$ is the row count, $S_x = \sum x$, $S_y = \sum y$, $S_{xy} = \sum xy$, $S_{xx} = \sum x^2$ and $S_{yy} = \sum y^2$. Then return one row with `r_squared` $= r \cdot r$, rounded to 4 decimal places.
```hint Round once
Keep full precision until the last step. Rounding r before you square it can move the fourth decimal of the result.
```
### Input Table(s)
Table `paired`:
| Column | Type | Meaning |
|---|---|---|
| `x` | DECIMAL(10,4) | First value of an observed pair |
| `y` | DECIMAL(10,4) | Second value of the same pair |
The table has no primary key. Each row is one observation.
### Output Contract
- Exactly one row with one column, `r_squared`.
- `r_squared` is the square of r, rounded to 4 decimal places with `ROUND(..., 4)`.
- The result is a single row, so no ordering applies.
### Example
Input `paired`:
| x | y |
|---|---|
| 31 | 45.6 |
| 13.4 | 24.6 |
| 41.8 | 65.3 |
| 37.9 | 62.8 |
| 21.8 | 28.8 |
| 36.8 | 48.5 |
| 3.9 | 16.2 |
| 26.1 | 45.4 |
Expected output:
| r_squared |
|---|
| 0.9183 |
For these 8 rows the sums are $n = 8$, $S_x = 212.7$, $S_y = 337.2$, $S_{xy} = 10513.66$, $S_{xx} = 6850.11$ and $S_{yy} = 16397.74$. The formula gives r of about 0.958284, and its square is about 0.918307, which rounds to 0.9183.
### Constraints and Clarifications
- `paired` has at least two rows.
- Neither `x` nor `y` is ever NULL.
- Neither column is constant, so both quantities under the square roots are positive and the denominator is never zero.
- Duplicate rows are separate observations. Each copy counts toward $n$ and toward every sum.
- The output is one row, so there are no ties to break.
Overview: Write a PostgreSQL query that computes the Pearson correlation of two paired numeric columns from raw sums, squares it to get R-squared, and rounds the result to four decimals. It tests single-pass aggregation, translating a regression statistic into SQL, and rounding only at the final step.
The table paired holds paired observations x and y, one pair per row. Compute the Pearson correlation r with the raw-sum formula
r=nSxx−Sx2nSyy−Sy2nSxy−SxSy
where n is the row count, Sx=∑x, Sy=∑y, Sxy=∑xy, Sxx=∑x2 and Syy=∑y2. Then return one row with r_squared=r⋅r, rounded to 4 decimal places.
Input Table(s)
Table paired:
Column
Type
Meaning
x
DECIMAL(10,4)
First value of an observed pair
y
DECIMAL(10,4)
Second value of the same pair
The table has no primary key. Each row is one observation.
Output Contract
Exactly one row with one column,
r_squared
.
r_squared
is the square of r, rounded to 4 decimal places with
ROUND(..., 4)
.
The result is a single row, so no ordering applies.
Example
Input paired:
x
y
31
45.6
13.4
24.6
41.8
65.3
37.9
62.8
21.8
28.8
36.8
48.5
3.9
16.2
26.1
45.4
Expected output:
r_squared
0.9183
For these 8 rows the sums are n=8, Sx=212.7, Sy=337.2, Sxy=10513.66, Sxx=6850.11 and Syy=16397.74. The formula gives r of about 0.958284, and its square is about 0.918307, which rounds to 0.9183.
Constraints and Clarifications
paired
has at least two rows.
Neither
x
nor
y
is ever NULL.
Neither column is constant, so both quantities under the square roots are positive and the denominator is never zero.
Duplicate rows are separate observations. Each copy counts toward
n
and toward every sum.
The output is one row, so there are no ties to break.