COVAR_POP()
The COVAR_POP()
function in MySQL calculates the population covariance between two sets of numeric values. Covariance enables us to measure the degree to which two variables modify together.
This function is regularly used in statistical analysis to understand relationships between variables. A positive covariance indicates that both variables tend to increase together, while a negative covariance indicates an inverse relationship.
Syntax
COVAR_POP(expression1, expression2)
expression1
: The first numeric data set.expression2
: The second numeric data set.
Both expressions must contain the same number of rows.
Example
Suppose we have a table sales
that records the number of units sold and the corresponding revenue for different products:
product_id | units_sold | revenue |
---|---|---|
1 | 100 | 1000 |
2 | 200 | 2500 |
3 | 150 | 1500 |
4 | 300 | 4000 |
To calculate the population covariance between units_sold
and revenue
, the following query including COVAR_POP()
can be used:
SELECT COVAR_POP(units_sold, revenue) AS population_covarianceFROM sales;
The output of the above code will be as follows:
+----------------------+| population_covariance |+----------------------+| 666666.6667 |+----------------------+
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn MySQL on Codecademy
- Skill path
Analyze Data with SQL
Learn to analyze data with SQL and prepare for technical interviews.Includes 9 CoursesWith CertificateBeginner Friendly17 hours - Free course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner Friendly5 hours