MySQL - STDDEV_SAMP() Function



The MySQL STDDEV_SAMP() function calculates and returns the sample standard deviation for the values in a particular column.

If the specified row(s) doesn't exist this function returns NULL. This result of this function will be the square root of VAR_SAMP().

Syntax

Following is the syntax of MySQL STDDEV_SAMP() function −

STDDEV_SAMP(expr);

Parameters

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

  • expr: A numeric expression or a particular column in a table for which we want to calculate the sample standard deviation.

Return value

This function returns the sample standard deviation for the values 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)
);

Execute the below query to insert 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 );

To fetch all the records that are inserted in the CUSTOMERS table, execute the following query −

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

The following query uses MySQL STDDEV_SAMP() function to calculate the sample standard deviation of AGE of all the customers −

SELECT STDDEV_SAMP(AGE) From CUSTOMERS;

This will produce the following result −

STDDEV_SAMP(AGE)
3.309438162646486

Here, we are calculating sample standard deviation on SALARY column −

SELECT STDDEV_SAMP(SALARY) From CUSTOMERS;

The output for the query above is produced as given below −

STDDEV_SAMP(SALARY)
3415.650255319866

The result of the below query will be NULL because there are no values present for ID = 10 in the table −

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

This will produce the following result −

Advertisements
VAR_SAMP(SALARY)
NULL