Compute pairwise Pearson correlations between three metrics in SQL
Quick Overview
Write a SQL query that computes the Pearson correlation for each pair of three metric columns from raw sums and returns one labeled row per pair. It tests single-pass aggregation, reshaping one aggregate row into several output rows, and guarding against a zero-variance column.
Compute pairwise Pearson correlations between three metrics in SQL
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
The `metrics` table records three metrics, `m1`, `m2` and `m3`, for each observation. Compute the Pearson correlation coefficient for each of the three pairs (`m1_m2`, `m1_m3`, `m2_m3`) using 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 number of rows, $S_x$ and $S_y$ are the column sums, $S_{xx}$ and $S_{yy}$ are the sums of squares, and $S_{xy}$ is the sum of the row-by-row products. Return three rows, one per pair.
### Input Table(s)
`metrics`
| Column | Type | Meaning |
|---|---|---|
| m1 | DECIMAL(10,4) | First metric for the observation |
| m2 | DECIMAL(10,4) | Second metric for the observation |
| m3 | DECIMAL(10,4) | Third metric for the observation |
Each row is one observation. The table has no key column.
### Output Contract
Exactly three rows with these columns, in this order:
| Column | Definition | Rounding |
|---|---|---|
| pair | The label `'m1_m2'`, `'m1_m3'` or `'m2_m3'` | none |
| correlation | Pearson r for that pair of columns | 4 decimals |
Sort by `pair` ascending.
### Example
Input `metrics`:
| m1 | m2 | m3 |
|---|---|---|
| 30.4 | 46.6 | 11.3 |
| 34.5 | 50.8 | 6.2 |
| 19.4 | 38.3 | 26.4 |
| 16.5 | 14.8 | 31.5 |
| 13.9 | 29.8 | 31.1 |
| 37.5 | 42.4 | 4.4 |
| 20.2 | 32.4 | 27.6 |
| 40.0 | 60.2 | 7.0 |
| 32.2 | 38.8 | 19.4 |
| 46.2 | 54.6 | -5.0 |
Expected result:
| pair | correlation |
|---|---|
| m1_m2 | 0.8475 |
| m1_m3 | -0.9665 |
| m2_m3 | -0.8499 |
`m1` and `m2` rise together, so their correlation is strongly positive. `m3` falls as either of the others rises (the row with the largest `m1` has the only negative `m3`), so both of its correlations are negative, and its link with `m1` is the tightest of the three.
### Constraints and Clarifications
- The table has at least 2 rows.
- `m1`, `m2` and `m3` are never NULL.
- Rows may repeat. Every row is an observation and counts toward the sums.
- If a column has the same value in every row, its variance is 0 and its correlations are undefined. Return NULL for those pairs; the query must not fail with a division-by-zero error.
```hint One scan is enough
The output has three rows, but every quantity in the formula is a plain sum over the table.
```
Overview: Write a SQL query that computes the Pearson correlation for each pair of three metric columns from raw sums and returns one labeled row per pair. It tests single-pass aggregation, reshaping one aggregate row into several output rows, and guarding against a zero-variance column.
The metrics table records three metrics, m1, m2 and m3, for each observation. Compute the Pearson correlation coefficient for each of the three pairs (m1_m2, m1_m3, m2_m3) using the raw-sum formula
r=nSxx−Sx2nSyy−Sy2nSxy−SxSy
where n is the number of rows, Sx and Sy are the column sums, Sxx and Syy are the sums of squares, and Sxy is the sum of the row-by-row products. Return three rows, one per pair.
Input Table(s)
metrics
Column
Type
Meaning
m1
DECIMAL(10,4)
First metric for the observation
m2
DECIMAL(10,4)
Second metric for the observation
m3
DECIMAL(10,4)
Third metric for the observation
Each row is one observation. The table has no key column.
Output Contract
Exactly three rows with these columns, in this order:
Column
Definition
Rounding
pair
The label 'm1_m2', 'm1_m3' or 'm2_m3'
none
correlation
Pearson r for that pair of columns
4 decimals
Sort by pair ascending.
Example
Input metrics:
m1
m2
m3
30.4
46.6
11.3
34.5
50.8
6.2
19.4
38.3
26.4
16.5
14.8
31.5
13.9
29.8
31.1
37.5
42.4
4.4
20.2
32.4
27.6
40.0
60.2
7.0
32.2
38.8
19.4
46.2
54.6
-5.0
Expected result:
pair
correlation
m1_m2
0.8475
m1_m3
-0.9665
m2_m3
-0.8499
m1 and m2 rise together, so their correlation is strongly positive. m3 falls as either of the others rises (the row with the largest m1 has the only negative m3), so both of its correlations are negative, and its link with m1 is the tightest of the three.
Constraints and Clarifications
The table has at least 2 rows.
m1
,
m2
and
m3
are never NULL.
Rows may repeat. Every row is an observation and counts toward the sums.
If a column has the same value in every row, its variance is 0 and its correlations are undefined. Return NULL for those pairs; the query must not fail with a division-by-zero error.