Compute the Pearson correlation coefficient in SQL from raw sums
Quick Overview
Write a PostgreSQL query that computes the Pearson correlation coefficient of two paired numeric columns from raw sums and rounds it to four decimals. It tests single-pass aggregation, turning a statistics formula into SQL, and handling numeric types, NULLs and zero variance.
Compute the Pearson correlation coefficient in SQL from raw sums
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
The table `points` holds paired observations `x` and `y`, one pair per row. Compute the Pearson correlation coefficient between `x` and `y` 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$. Return one row with the coefficient in a column named `correlation`, rounded to 4 decimal places.
```hint Rounding in PostgreSQL
`ROUND(value, 4)` exists for `numeric` but not for `double precision`. Check the type of your final expression before you round it.
```
### Input Table(s)
Table `points`:
| 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, `correlation`.
- `correlation` is the Pearson coefficient rounded to 4 decimal places with `ROUND(..., 4)`.
- The result is a single row, so no ordering applies.
### Example
Input `points`:
| x | y |
|---|---|
| 31.8 | 58.7 |
| 17.9 | 37.7 |
| 39.4 | 65.9 |
| 27.6 | 38.6 |
| 8.1 | 16.2 |
| 47.5 | 72.2 |
| 6.2 | 16.4 |
| 23.6 | 30.5 |
Expected output:
| correlation |
|---|
| 0.9620 |
For these 8 rows the sums are $n = 8$, $S_x = 202.1$, $S_y = 336.2$, $S_{xy} = 10585.51$, $S_{xx} = 6563.03$ and $S_{yy} = 17374.24$. Plugging them into the formula gives r of about 0.962006, which rounds to 0.9620.
### Constraints and Clarifications
- `points` 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 coefficient of two paired numeric columns from raw sums and rounds it to four decimals. It tests single-pass aggregation, turning a statistics formula into SQL, and handling numeric types, NULLs and zero variance.
The table points holds paired observations x and y, one pair per row. Compute the Pearson correlation coefficient between x and y 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. Return one row with the coefficient in a column named correlation, rounded to 4 decimal places.
Input Table(s)
Table points:
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,
correlation
.
correlation
is the Pearson coefficient rounded to 4 decimal places with
ROUND(..., 4)
.
The result is a single row, so no ordering applies.
Example
Input points:
x
y
31.8
58.7
17.9
37.7
39.4
65.9
27.6
38.6
8.1
16.2
47.5
72.2
6.2
16.4
23.6
30.5
Expected output:
correlation
0.9620
For these 8 rows the sums are n=8, Sx=202.1, Sy=336.2, Sxy=10585.51, Sxx=6563.03 and Syy=17374.24. Plugging them into the formula gives r of about 0.962006, which rounds to 0.9620.
Constraints and Clarifications
points
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.