Skip to content
Related Articles

Related Articles

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 :

STUDENTIDSTUDENTNAMEROLLSUB1MARKSUB2MARKSUB3MARKTOTALMARKS
1Amit Jana10100858095260
2Labanya Mallick11000818995265
3Virat Sharma12000758390248
4Sayani Samanta13000959099284
5Riyanka Panda14000708788245
6Ritika Shah15000788990257

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_IDEMPLOYEE_NAMEWORKING_ATWORK_LOCATIONJOINING_DATEANNUAL_INCOME
1Amit KhanXYZ DigitalKolkata2019-10-06350000
2Shreetama PalABC Corp.Kolkata2018-12-16500000
3Aniket SharmaPQR Soln.Delhi2020-01-11300000
4Maitree JanaXYZ DigitalKolkata2019-05-01400000
5Priyanka OjhaABC Corp.Delhi2019-02-13350000
6Sayani MitraXYZ DigitalKolkata2019-09-15320000
7Nitin DeyPQR Soln.Delhi2019-10-06250000
8Sujata SamantaPQR Soln.Kolkata2020-10-06350000
9Sudip MajhiABC Corp.Delhi2018-10-30600000
10Sanjoy KohliXYZ DigitalDelhi2019-04-18450000

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
My Personal Notes arrow_drop_up
Recommended Articles
Page :