VARIANCE() function in MySQL
Last Updated :
11 Jan, 2021
Sometimes we need to calculate population Standard variance of an expression in MySQL. VARIANCE() function can be used for this purpose in MySQL. It returns NULL if in the given expression no matching rows are found.
Syntax :
VARIANCE(expr);
Parameter : This method accepts only one parameter.
- expr: Input expression from which we want to calculate standard variance.
Returns : It returns the population standard variance.
Example-1 :
Finding standard variance of sub1mark column from the given StudentMarks table using VARIANCE Function.
Creating a StudentMarks table :
CREATE TABLE StudentMarks
(
StudentId INT AUTO_INCREMENT,
StudentName VARCHAR(100) NOT NULL,
Roll INT NOT NULL,
Sub1Mark INT NOT NULL,
Sub2Mark INT NOT NULL,
Sub3Mark INT NOT NULL,
TotalMarks INT NOT NULL,
PRIMARY KEY(StudentId )
);
Inserting data into the Table :
INSERT INTO StudentMarks
(StudentName, Roll, Sub1Mark, Sub2Mark, Sub3Mark, TotalMarks)
VALUES
('Amit Jana', 10100, 85, 80, 95, 260),
('Labanya Mallick', 11000, 81, 89, 95, 265),
('Virat Sharma', 12000, 75, 83, 90, 248),
('Sayani Samanta', 13000, 95, 90, 99, 284),
('Riyanka Panda', 14000, 70, 87, 88, 245),
('Ritika Shah', 15000, 78, 89, 90, 257);
To verify used the following command as follows.
SELECT * from StudentMarks;
Output :
STUDENTID | STUDENTNAME | ROLL | SUB1MARK | SUB2MARK | SUB3MARK | TOTALMARKS |
---|
1 | Amit Jana | 10100 | 85 | 80 | 95 | 260 |
2 | Labanya Mallick | 11000 | 81 | 89 | 95 | 265 |
3 | Virat Sharma | 12000 | 75 | 83 | 90 | 248 |
4 | Sayani Samanta | 13000 | 95 | 90 | 99 | 284 |
5 | Riyanka Panda | 14000 | 70 | 87 | 88 | 245 |
6 | Ritika Shah | 15000 | 78 | 89 | 90 | 257 |
Now we are going to find standard variance of sub1mark column.
SELECT VARIANCE(Sub1Mark) as Sub1Variance
FROM StudentMarks;
Output :
SUB1VARIANCE |
---|
62.88888888888891 |
Example-2
Now we are going to find standard variance of total marks column.
SELECT VARIANCE(TotalMarks) as VarianceOfTotalMarks
FROM StudentMarks;
Output :
VARIANCEOFTOTALMARKS |
---|
163.13888888888877 |
Example-3 : In this example we are going to find the population standard variance of Income of Employee who are working in the company 'ABC Corp.' To demonstrate create a table named EmloyeeDetails.
CREATE TABLE EmployeeDetails(
Employee_Id INT AUTO_INCREMENT,
Employee_Name VARCHAR(100) NOT NULL,
Working_At VARCHAR(20) NOT NULL,
Work_Location VARCHAR(20) NOT NULL,
Joining_Date DATE NOT NULL,
Annual_Income INT NOT NULL,
PRIMARY KEY(Employee_Id )
);
Inserting data into the Table :
INSERT INTO
EmployeeDetails(Employee_Name, Working_At, Work_Location, Joining_Date, Annual_Income )
VALUES
('Amit Khan', 'XYZ Digital', 'Kolkata', '2019-10-06', 350000 ),
('Shreetama Pal', 'ABC Corp.', 'Kolkata', '2018-12-16', 500000 ),
('Aniket Sharma', 'PQR Soln.', 'Delhi', '2020-01-11', 300000 ),
('Maitree Jana', 'XYZ Digital', 'Kolkata', '2019-05-01', 400000 ),
('Priyanka Ojha', 'ABC Corp.', 'Delhi', '2019-02-13', 350000 ),
('Sayani Mitra', 'XYZ Digital', 'Kolkata', '2019-09-15', 320000 ),
('Nitin Dey', 'PQR Soln.', 'Delhi', '2019-10-06', 250000 ),
('Sujata Samanta', 'PQR Soln.', 'Kolkata', '2020-10-06', 350000 ),
('Sudip Majhi', 'ABC Corp.', 'Delhi', '2018-10-30', 600000 ),
('Sanjoy Kohli', 'XYZ Digital', 'Delhi', '2019-04-18', 450000 ) ;
To verify used the following command as follows.
Select * FROM EmployeeDetails;
Output :
EMPLOYEE_ID | EMPLOYEE_NAME | WORKING_AT | WORK_LOCATION | JOINING_DATE | ANNUAL_INCOME |
---|
1 | Amit Khan | XYZ Digital | Kolkata | 2019-10-06 | 350000 |
2 | Shreetama Pal | ABC Corp. | Kolkata | 2018-12-16 | 500000 |
3 | Aniket Sharma | PQR Soln. | Delhi | 2020-01-11 | 300000 |
4 | Maitree Jana | XYZ Digital | Kolkata | 2019-05-01 | 400000 |
5 | Priyanka Ojha | ABC Corp. | Delhi | 2019-02-13 | 350000 |
6 | Sayani Mitra | XYZ Digital | Kolkata | 2019-09-15 | 320000 |
7 | Nitin Dey | PQR Soln. | Delhi | 2019-10-06 | 250000 |
8 | Sujata Samanta | PQR Soln. | Kolkata | 2020-10-06 | 350000 |
9 | Sudip Majhi | ABC Corp. | Delhi | 2018-10-30 | 600000 |
10 | Sanjoy Kohli | XYZ Digital | Delhi | 2019-04-18 | 450000 |
Now we are going to find population standard variance of annual Income for those Employee who are working in 'ABC Corp.'
SELECT 'ABC Corp.' AS 'Company_Name',
VARIANCE(Annual_Income) as VarianceOfAnnualIncome
FROM EmployeeDetails where WORKING_AT = 'ABC Corp.';
Output :
COMPANY_NAME | VARIANCEOFANNUALINCOME |
---|
ABC Corp. | 10555555555.555557 |
Similar Reads
RAND() Function in MySQL
The RAND() function in MySQL is used to a return random floating-point value V in the range 0 <= V < 1.0. If we want to obtain a random integer R in the range i <= R < j, we have to use the expression : FLOOR(i + RAND() * (j â i)). Syntax : RAND(N) Parameter : This method accepts only on
3 min read
SPACE() Function in MySQL
SPACE() function in MySQL is used to return a string consisting of specified empty space characters. Syntax : SPACE(num) Parameter : This method accepts one parameter as mentioned above and described below : num : It is an integer which indicates how many spaces are being contained. Returns : It ret
1 min read
REPLACE() Function in MySQL
The REPLACE() function in MySQL is a powerful tool for string manipulation, allowing users to substitute specific substrings within a larger string. This functionality is particularly useful in various applications such as updating text data, cleaning up input or adjusting content in a database. In
3 min read
RADIANS() Function in MySQL
RADIANS() function in MySQL is used to convert the degree values into radians. The formula for converting degree to radian is : 180 degrees = Ï radian Syntax : RADIANS(X) Parameter : This method accepts only one parameter. X : The degree value which we convert to radian. Returns : It returns equival
2 min read
TAN() Function in MySQL
TAN() function : This function in MySQL is used to return the tangent of a specified number. In any right triangle, the tangent of an angle is the length of the opposite side divided by the length of the adjacent side. Similarly, this can also be defined as tangent of x is the sine of x divided by t
1 min read
TRUNCATE() Function in MySQL
The TRUNCATE() function in MySQL is a valuable tool for manipulating numerical values by removing their decimal parts without rounding. It allows us to limit the precision of numbers to a specified number of decimal places or even truncate them to the nearest integer. In this article, We will learn
6 min read
SIGN() Function in MySQL
SIGN() function in MySQL is used to return the sign of the given number. It returns 1 if the number is positive, -1 if the number is negative and 0 for zero. Syntax : SIGN(X) Parameter : SIGN() function accepts one parameter as input and will give you the results in values like Positive(+1),Negative
1 min read
YEAR() Function in MySQL
YEAR() function in MySQL is used to find year from the given date. If the date is NULL, the YEAR() function will return NULL. Otherwise, it returns value range from 1000 to 9999. Syntax : YEAR(date) Parameter : This method accepts one parameter as mentioned above and described below : date : The dat
3 min read
TIME() Function in MySQL
The TIME() function in MySQL is used to extract the time portion from a date or datetime expression, returning the time in the format 'HH:MM'. This function is particularly useful when working with time components in databases, such as scheduling or logging systems. In this article, We will learn ab
4 min read
PI() function in MySQL
PI() function in MySQL is used to return the Pi value. The default number of decimal places displayed is seven, but MySQL uses the full double-precision value internally. Syntax : PI() Parameter : This method does not accept any parameter. Returns : It returns the Pi value i.e. 3.141593. Example-1 :
2 min read