VAR_SAMP() function in MySQL
Last Updated :
30 Dec, 2020
VAR_SAMP() function in MySQL is used to calculate sample variance of an expression.
Syntax :
VAR_SAMP(expr);
Parameter : This method accepts only one parameter.
-
expr : Input expression from which we want to calculate sample variance.
Returns : It returns the sample variance.
Example-1 :
Finding sample variance of RunScored column from the given Player table using VAR_SAMP Function.
Creating a Player table :
CREATE TABLE Player
(
PlayerId INT AUTO_INCREMENT,
PlayerName VARCHAR(100) NOT NULL,
RunScored INT NOT NULL,
WicketsTaken INT NOT NULL,
PRIMARY KEY(PlayerId)
);
Inserting data into the Table :
INSERT INTO Player
(PlayerName, RunScored, WicketsTaken )
VALUES
('
KL Rahul', 52, 0 ),
('Hardik Pandya', 30, 1 ),
('Ravindra Jadeja', 18, 2 ),
('Washington Sundar', 10, 1),
('D Chahar', 11, 2 ),
('Mitchell Starc', 0, 3);
To verify used the following command as follows.
SELECT * from Player ;
Output :
PLAYERID |
PLAYERNAME |
RUNSCORED |
WICKETSTAKEN |
1 |
KL Rahul |
52 |
0 |
2 |
Hardik Pandya |
30 |
1 |
3 |
Ravindra Jadeja |
18 |
2 |
4 |
Washington Sundar |
10 |
1 |
5 |
D Chahar |
11 |
2 |
6 |
Mitchell Starc |
0 |
3 |
Now we are going to find sample variance for RunScored column.
SELECT VAR_SAMP(RunScored ) as Run_Variance
FROM Player ;
Output :
RUN_VARIANCE |
341.7666666666667 |
Example-2 :
Now we are going to find sample variance of WicketsTaken column.
SELECT VAR_SAMP(WicketsTaken) as Wicket_Variance
FROM Player ;
Output :
Example-3 :
In this example we are going to find the sample variance of Income of Employee who are working in the company 'PQR Soln.' 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 sample variance of annual Income for those Employee who are working in 'PQR Soln.'
SELECT 'PQR Soln.' AS 'Company_Name',
VAR_SAMP(Annual_Income) as VarianceOfAnnualIncome
FROM EmployeeDetails where WORKING_AT = 'PQR Soln.';
Output :
COMPANY_NAME |
VARIANCEOFANNUALINCOME |
PQR Soln. |
2500000000 |
Similar Reads
VAR_POP() function in MySQL
VAR_POP() function in MySQL is used to calculate population standard variance of an expression. Syntax : VAR_POP(expr); Parameter : This method accepts only one parameter. expr : Input expression from which we want to calculate population standard variance. Returns : It returns the population standa
3 min read
STDDEV_SAMP() function in MySQL
STDDEV_SAMP() function in MySQL is used to calculate sample standard deviation of an expression. Syntax : STDDEV_SAMP(expr); Parameter : This method accepts only one parameter. expr : Input expression from which we want to calculate sample standard deviation. Returns : It returns the population stan
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
SUM() Function in MySQL
The SUM() function in MySQL is a powerful aggregate function used to calculate the total sum of values in a numeric column. By summing up the values in the specified column, this function helps in generating overall totals and performing calculations that provide meaningful insights from our data. I
4 min read
STRCMP() Function in MySQL
STRCMP() function in MySQL is used to compare two strings. If both of the strings are same then it returns 0, if the first argument is smaller than the second according to the defined order it returns -1 and it returns 1 when the second one is smaller the first one. Syntax : STRCMP(Str1, Str2) Param
3 min read
SQRT() Function in MySQL
The SQRT() function in MySQL calculates the square root of a non-negative number, returning NULL for negative inputs. It is a built-in function that provides high precision and is optimized for performance and making it ideal for mathematical and scientific applications.In the article, we will cover
3 min read
RPAD() Function in MySQL
RPAD() function in MySQL is used to pad or add a string to the right side of the original string. Syntax : RPAD(str, len, padstr) Parameter : This function accepts three parameter as mentioned above and described below : str : The actual string which is to be padded. If the length of the original st
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
VARIANCE() function in MySQL
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:
3 min read
STD() function in MySQL
With the help of STD() function we can calculate population Standard deviation of an expression in MySQL. But, if there are no matching rows in the given expression it returns Null. Syntax : STD(expr); Parameter : This method accepts only one parameter. expr : Input expression from which we want to
3 min read