Open In App

STDDEV() function in MySQL

Improve
Improve
Like Article
Like
Save
Share
Report

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

Last Updated : 30 Dec, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads