Example 1
Measure the correlation between ad spend and conversions across five campaigns
The five (ad_spend, conversions) pairs are (1,2), (2,3), (3,5), (4,4), (5,6). Mean ad spend is 3; mean conversions is 4. The sum of cross-deviations Σ(xi−3)(yi−4) is (−2)(−2)+(−1)(−1)+(0)(1)+(1)(0)+(2)(2) = 4+1+0+0+4 = 9. Both variance sums Σ(xi−3)² and Σ(yi−4)² equal 10. CORR = 9 / √(10×10) = 9/10 = 0.9 — a strong positive linear relationship. COVAR_SAMP = 9 / (5−1) = 2.25. COVAR_POP = 9 / 5 = 1.8. A correlation of 0.9 suggests ad spend and conversions move closely together, though correlation alone does not establish causation. To compute CORR in engines without a built-in function, use (AVG(ad_spend conversions) − AVG(ad_spend) AVG(conversions)) / (STDDEV_POP(ad_spend) * STDDEV_POP(conversions)).
CREATE TABLE campaign_data (ad_spend INT, conversions INT);
INSERT INTO
campaign_data
VALUES
(1, 2),
(2, 3),
(3, 5),
(4, 4),
(5, 6);SELECT
CORR(conversions, ad_spend) AS correlation,
COVAR_SAMP(conversions, ad_spend) AS covar_samp,
COVAR_POP(conversions, ad_spend) AS covar_pop
FROM
campaign_data;| correlation | covar_samp | covar_pop |
|---|---|---|
| 0.9 | 2.25 | 1.8 |
Only PostgreSQL supports CORR, COVAR_SAMP, and COVAR_POP as built-in aggregates.