STDDEV() function in MySQL
Last Updated :
30 Dec, 2020
Sometimes we need to calculate population Standard deviation of an expression in MySQL.
STDDEV() function can be used for this purpose in MySQL. It returns NULL if no matching rows found in the given expression.
Syntax :
STDDEV(expr);
Parameter : This method accepts only one parameter.
-
expr : Input expression from which we want to calculate standard deviation .
Returns : It returns the population standard deviation.
Example-1 :
Finding standard deviation of sub1mark column from the given StudentMarks table using STDDEV 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 deviation of sub1mark column.
SELECT STDDEV(Sub1Mark) as Sub1StandardDeviation
FROM StudentMarks;
Output :
SUB1STANDARDDEVIATION |
7.930251502246881 |
Example-2
Now we are going to find population standard deviation of total marks column.
SELECT STDDEV(TotalMarks) as StdDevOfTotalMarks
FROM StudentMarks;
Output :
STDDEVOFTOTALMARKS |
12.772583485297279 |
Example-3 : In this example we are going to find the population standard deviation of Income of Employee who are working in the company ‘XYZ Digital’. 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 deviation of annual Income for those Employee who are working in ‘XYZ Digital’.
SELECT STDDEV(Annual_Income) as StdDevOfAnnualIncome
FROM EmployeeDetails where WORKING_AT = 'XYZ Digital';
Output :
STDDEVOFANNUALINCOME |
49497.474683058324 |
Similar Reads
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 cove
4 min read
SOUNDEX() Function in MySQL
SOUNDEX() function in MySQL is used to return a phonetic representation of a string. The phonetic represents the way the string will sound. The SOUNDEX function helps to compare words that are spelled differently, but sound alike in English. Syntax : SOUNDEX(str) Parameter : SOUNDEX() function accep
3 min read
SYSDATE() function in MySQL
SYSDATE() function in MySQL is used to return the current date and time in YYYY-MM-DD HH:MM:SS or YYYYMMDDHHMMSS.uuuuuu format depending on the context of the function. Syntax : SYSDATE() Parameter : This method does not accept any parameter. Returns : It returns the current date and time value. Exa
1 min read
QUOTE () function in MySQL
QUOTE() : This function in MySQL is used to return a result that can be used as a properly escaped data value in an SQL statement. The string is returned enclosed by single quotation marks and with each instance of backslash (\), single quote ('), ASCII NULL, and Control+Z preceded by a backslash. I
2 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
ORD() Function in MySQL
ORD() function in MySQL is used to find the code of the leftmost character in a string . If the leftmost character is not a multibyte character, it returns ASCII value. And if the leftmost character of the string str is a multibyte character, ORD returns the code for that character, calculated from
3 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
OCT() function in MySQL
OCT() function in MySQL is used to convert decimal number to octal. It returns equivalent octal value of a decimal number. Syntax : OCT(number) Parameter : This method accepts only one parameter. number : The decimal number which we want to convert. Returns : It returns octal value of a decimal numb
2 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
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