SQL Tutorial SQL Advanced SQL Database SQL References

Oracle STDDEV_SAMP() Function



The Oracle (PL/SQL) STDDEV_SAMP() function returns the statistical standard deviation of all values in the specified expression.

This function differs from STDDEV() in that STDDEV() returns zero when it has only 1 row of input data, whereas STDDEV_SAMP() returns null.

Syntax

The syntax for using STDDEV_SAMP() function is given below:

SELECT STDDEV_SAMP(column_name) 
FROM table_name
WHERE condition(s);

The Oracle (PL/SQL) GROUP BY clause is used to arrange result table into identical groups when one or more columns are used. Please note that it is must to include those column names in a GROUP BY clause which are not encapsulated within the STDDEV_SAMP() function. See the syntax below:

SELECT column1, column2, ...
       STDDEV_SAMP(column_name) 
FROM table_name
WHERE condition(s)
GROUP BY column1, column2, ...;

Parameters

column1, column2, ... Specify the column names that are not encapsulated within the STDDEV_SAMP() function. It must be included in the GROUP BY clause.
column_name Specify the column or expression whose standard deviation need to be returned.
table_name Specify the table name from where the records need to retrieved.
WHERE condition(s) Optional. Specify the condition(s). Records are selected based upon specified condition(s).

Return Value

Returns the statistical standard deviation of all values in the specified expression.

Example - With Single Column

Consider a database table called Employee with the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

To get the standard deviation of salary of all employees whose age is greater than 25, the following query can be used:

SELECT STDDEV_SAMP(Salary) AS STDDEV_SAMP_Salary 
FROM Employee
WHERE Age > 25;

This will produce the result as shown below:

STDDEV_SAMP_Salary
150


Example - Using Formula

The expression contained within the STDDEV_SAMP() function does not need to be a single field. A formula can also be used with this function. For example - Consider a bonus (15% of Salary) is given to each employee, and to calculate the standard deviation of bonus amount, the following query can be used:

SELECT STDDEV_SAMP(Salary * 0.15) AS STDDEV_SAMP_BonusAmount
FROM Employee;

This will produce the result as shown below:

STDDEV_SAMP_BonusAmount
21.4330352493528083592262356536765496797


Example - Using GROUP BY

To get the standard deviation of salary of employees group by their age, the following query can be used:

SELECT Age, STDDEV_SAMP(Salary) AS STDDEV_SAMP_SalaryByAge 
FROM Employee
GROUP BY Age;

This result of the above code will be:

AgeSTDDEV_SAMP_SalaryByAge
24NULL
25NULL
27NULL
28141.421356237309504880168872420969807857
30NULL

❮ Oracle Functions