SQL CORR
The function that computes the actual correlation coefficient from the previous lesson.
What & Why
CORR(y, x) computes the Pearson correlation coefficient between two numeric columns — the exact -1-to-+1 number the previous lesson described.
See How It Works
BUSINESS QUESTION
Marketing wants the correlation between email opens and clicks.
| id | campaign_id | sent_at | recipients | opens | clicks | unsubscribes | bounces |
|---|---|---|---|---|---|---|---|
| 201 | 1 | 2024-01-20 15:00:00+00 | 1200 | 540 | 180 | 9 | 24 |
| 202 | 2 | 2024-02-15 16:30:00+00 | 800 | 420 | 96 | 5 | 12 |
| 203 | 3 | 2024-03-18 13:00:00+00 | 1500 | 610 | 225 | 14 | 31 |
| 204 | 4 | 2024-04-08 14:00:00+00 | 0 | 0 | 0 | 0 | 0 |
EXAMPLE QUERY
SELECT
ROUND(CORR(opens, clicks)::numeric, 4) AS opens_clicks_correlation
FROM marketing.email_sends;RESULT — opens and clicks
| opens_clicks_correlation |
|---|
| 0.9615 |
The rounded coefficient uses the four real email-send rows.
This lesson's practice is part of Pro.
Sign up free to try it on a real business scenario