Compute sample covariance in SQL with the raw-sum formula

Quick Overview

Write a PostgreSQL query that computes the sample covariance of two numeric columns with the raw-sum formula and rounds it to four decimals. It tests single-pass aggregation, numeric types that avoid integer division, and returning a clean result when the table has fewer than two rows.

Compute sample covariance in SQL with the raw-sum formula

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Given a table `paired` of numeric observations `x` and `y`, compute the sample covariance with the raw-sum formula $$\text{cov} = \frac{n \cdot S_{xy} - S_x \cdot S_y}{n(n-1)}$$ where $n$ is the row count, $S_{xy} = \sum xy$, $S_x = \sum x$ and $S_y = \sum y$. Return one row with `covariance` rounded to 4 decimals. ```hint Edge case Work out what the denominator becomes when the table holds a single row, and make sure the query still returns a row. ``` ### Input Table(s) Table `paired` | Column | Type | Meaning | |---|---|---| | x | DECIMAL(10,4) | First value of the pair | | y | DECIMAL(10,4) | Second value of the pair | The table has no primary key. Each row is one paired observation, and identical rows count as separate observations. ### Output Contract - Exactly one row with one column, `covariance`. - `covariance` is the value of the formula above, rounded to 4 decimal places. - The result always has a single row, so no ordering applies. ### Example Input `paired`: | x | y | |---|---| | 7.8 | 23.3 | | 26.8 | 27.5 | | 45.6 | 53.1 | | 16.7 | 29 | | 19.2 | 32.3 | | 31 | 42.9 | | 42 | 65.1 | | 5.7 | 20.2 | | 43.5 | 56.9 | Expected output: | covariance | |---| | 227.9731 | Here $n = 9$, $S_x = 238.3$, $S_y = 350.3$ and $S_{xy} = 11098.95$, so the numerator is ${9 \cdot 11098.95 - 238.3 \cdot 350.3 = 16414.06}$ and the denominator is ${9 \cdot 8 = 72}$. The ratio is 227.973055..., which rounds to 227.9731. ### Constraints and Clarifications - Neither `x` nor `y` is NULL. - Every row counts once in $n$ and in each sum, including repeated rows. - With fewer than two rows the sample covariance is undefined. Return one row with `covariance` set to NULL; the query must not fail with a division-by-zero error. - Compute the value from the raw-sum formula as written. A built-in such as `COVAR_SAMP` gives the same number after rounding, but the exercise is to build it from `COUNT` and `SUM`.

Overview: Write a PostgreSQL query that computes the sample covariance of two numeric columns with the raw-sum formula and rounds it to four decimals. It tests single-pass aggregation, numeric types that avoid integer division, and returning a clean result when the table has fewer than two rows.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Given a table paired of numeric observations x and y, compute the sample covariance with the raw-sum formula

cov=n⋅Sxy−Sx⋅Syn(n−1)\text{cov} = \frac{n \cdot S_{xy} - S_x \cdot S_y}{n(n-1)}

where nn is the row count, Sxy=∑xyS_{xy} = \sum xy, Sx=∑xS_x = \sum x and Sy=∑yS_y = \sum y. Return one row with covariance rounded to 4 decimals.

Input Table(s)

Table paired

ColumnTypeMeaning
xDECIMAL(10,4)First value of the pair
yDECIMAL(10,4)Second value of the pair

The table has no primary key. Each row is one paired observation, and identical rows count as separate observations.

Output Contract

  • Exactly one row with one column, covariance .
  • covariance is the value of the formula above, rounded to 4 decimal places.
  • The result always has a single row, so no ordering applies.

Example

Input paired:

xy
7.823.3
26.827.5
45.653.1
16.729
19.232.3
3142.9
4265.1
5.720.2
43.556.9

Expected output:

covariance
227.9731

Here n=9n = 9, Sx=238.3S_x = 238.3, Sy=350.3S_y = 350.3 and Sxy=11098.95S_{xy} = 11098.95, so the numerator is 9⋅11098.95−238.3⋅350.3=16414.06{9 \cdot 11098.95 - 238.3 \cdot 350.3 = 16414.06} and the denominator is 9⋅8=72{9 \cdot 8 = 72}. The ratio is 227.973055..., which rounds to 227.9731.

Constraints and Clarifications

  • Neither x nor y is NULL.
  • Every row counts once in nn and in each sum, including repeated rows.
  • With fewer than two rows the sample covariance is undefined. Return one row with covariance set to NULL; the query must not fail with a division-by-zero error.
  • Compute the value from the raw-sum formula as written. A built-in such as COVAR_SAMP gives the same number after rounding, but the exercise is to build it from COUNT and SUM .