SQL Covariance

Correlation's raw, unnormalized ancestor — same direction of relationship, but the number isn't bounded between -1 and 1.

What & Why

Covariance measures the same "do these move together" question as correlation, but without normalizing the result — the number's scale depends entirely on the units of the two variables, which makes it hard to compare across different pairs of variables. Correlation exists specifically to fix that by normalizing covariance into the -1-to-+1 range.

See How It Works

BUSINESS QUESTION

Measure sample covariance between email opens and clicks.

idcampaign_idsent_atrecipientsopensclicksunsubscribesbounces
20112024-01-20 15:00:00+001200540180924
20222024-02-15 16:30:00+0080042096512
20332024-03-18 13:00:00+0015006102251431
20442024-04-08 14:00:00+0000000
EXAMPLE QUERY
SELECT
  COVAR_SAMP(opens, clicks) AS opens_clicks_covariance,
  COUNT(*) AS sends
FROM marketing.email_sends
WHERE opens IS NOT NULL
  AND clicks IS NOT NULL;
RESULT — opens and clicks covariance
opens_clicks_covariancesends
26042.54

COVAR_SAMP uses the four representative opens/clicks pairs.

This lesson's practice is part of Pro.

Advanced business practice

Sign up free to try it on a real business scenario