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.

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

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=nSxy−SxSynSxx−Sx2 nSyy−Sy2r = \frac{n S_{xy} - S_x S_y}{\sqrt{n S_{xx} - S_x^2}\,\sqrt{n S_{yy} - S_y^2}}

where nn is the number of rows, SxS_x and SyS_y are the column sums, SxxS_{xx} and SyyS_{yy} are the sums of squares, and SxyS_{xy} is the sum of the row-by-row products. Return three rows, one per pair.

Input Table(s)

metrics

ColumnTypeMeaning
m1DECIMAL(10,4)First metric for the observation
m2DECIMAL(10,4)Second metric for the observation
m3DECIMAL(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:

ColumnDefinitionRounding
pairThe label 'm1_m2', 'm1_m3' or 'm2_m3'none
correlationPearson r for that pair of columns4 decimals

Sort by pair ascending.

Example

Input metrics:

m1m2m3
30.446.611.3
34.550.86.2
19.438.326.4
16.514.831.5
13.929.831.1
37.542.44.4
20.232.427.6
40.060.27.0
32.238.819.4
46.254.6-5.0

Expected result:

paircorrelation
m1_m20.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.