MySQL - STDDEV() Function



The MySQL STDDEV() function calculates and returns the population standard deviation of all the values in a particular column.

Standard deviation is the square root of the average of squared deviations of the items from their mean. Symbolically it is represented by σσ.

If the specified row(s) doesn't exist this function returns NULL.

Syntax

Following is the syntax of MySQL STDDEV() function −

STDDEV(expr);

Parameters

This method accepts a parameter. The same is described below −

  • expr: An expression or a particular column for which we want to calculate the population standard deviation.

Return value

This function calculates and returns the population standard deviation for the rows in a particular column.

Example

First of all, let us create a table named CUSTOMERS using the CREATE statement as follows −

CREATE TABLE CUSTOMERS (
   ID INT AUTO_INCREMENT,
   NAME VARCHAR(20) NOT NULL,
   AGE INT NOT NULL,
   ADDRESS CHAR (25),
   SALARY DECIMAL (18, 2),
   PRIMARY KEY (ID)
);

The following query inserts 7 records into the above created table −

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES 
(1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ),
(2, 'Khilan', 25, 'Delhi', 1500.00 ),
(3, 'Kaushik', 23, 'Kota', 2000.00 ),
(4, 'Chaitali', 25, 'Mumbai', 6500.00 ),
(5, 'Hardik', 27, 'Bhopal', 8500.00 ),
(6, 'Komal', 22, 'Hyderabad', 4500.00 ),
(7, 'Muffy', 24, 'Indore', 10000.00 );

Execute the following query to verify whether the records are inserted into CUSTOMERS table −

Select * From CUSTOMERS;

Following is the CUSTOMERS table −

ID NAME AGE ADDRESS SALARY
1 Ramesh 32 Ahmedabad 2000.00
2 Khilan 25 Delhi 1500.00
3 Kaushik 23 Kota 2000.00
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 8500.00
6 Komal 22 Hyderabad 4500.00
7 Muffy 24 Indore 10000.00

Here, we are using the MySQL STDDEV() function to calculate the population standard deviation of salaries of all the customers −

SELECT STDDEV(SALARY) From CUSTOMERS;

Output

This will produce the following result −

STD(SALARY)
3162.2776601683795

The result of the query will be NULL because there are no records in the CUSTOMERS table where the ID is equal to 10 −

SELECT STD(SALARY) From CUSTOMERS
WHERE ID = 10;

Output

This will produce the following result −

STD(SALARY)
NULL
Advertisements