Lessons › SQL › Deep Dive: Business Analytics Patterns
SQL lesson
Correlation with CORR()
CORR(y, x) gives one number from -1 to 1 that says how closely two columns move together.
Try this lesson free →Example
Do two numbers move together?
SELECT CORR(units_sold, price) AS correlation
FROM products| correlation |
|---|
| -0.863459396947833 |
Step by step
- units_sold is what you want to explain, price is what might explain it.
SELECT units_sold, price FROM products; - CORR(units_sold, price) returns one number for the whole table.
SELECT CORR(units_sold, price) FROM products; - AS gives the number a clear name in the report.
SELECT CORR(units_sold, price) AS correlation FROM products;
Practice
1. Show the correlation between units_sold and price, as correlation.
SELECT CORR(units_sold, price) AS correlation FROM products2. Show the correlation between units_sold and rating, as correlation.
SELECT CORR(units_sold, rating) AS correlation FROM products3. Show the correlation between units_sold and price, rounded to 2 decimal places, as correlation.
SELECT ROUND(CORR(units_sold, price)::numeric, 2) AS correlation FROM products4. Show two correlations: units_sold with price as price_link, and units_sold with rating as rating_link.
SELECT CORR(units_sold, price) AS price_link, CORR(units_sold, rating) AS rating_link FROM products5. Show the correlation between price and rating, as correlation.
SELECT CORR(price, rating) AS correlation FROM productsLearn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.
Try this lesson free →