STDDEV_SAMP
functionsupportedCore SQL functionsReturns the sample standard deviation (square root of sample variance) of non-NULL values. STDDEV and STDDEV_SAMP are aliases for the same function. See also STDDEV_POP, which returns the population standard deviation (square root of variance).
Syntax
supportedpartialnot supportednot scored
Aggregate function
{ STDDEV | STDDEV_SAMP } ( [ DISTINCT ] <expr1> )
Window function
{ STDDEV | STDDEV_SAMP } ( [ DISTINCT ] <expr1> ) OVER (
[ PARTITION BY <expr2> ]
[ ORDER BY <expr3> [ { ASC | DESC } ] [ NULLS { FIRST | LAST } ] [ <window_frame> ] ]
)
Parameters
| Name | Required | Description |
|---|---|---|
expr1 | required | An expression that evaluates to a numeric value. This is the expression on which the standard deviation is calculated. |
expr2 | required | This is the optional expression to partition by. |
expr3 | required | This is the optional expression to order by within each partition. |
Returns
The data type of the returned value is DOUBLE. If all records inside a group are NULL, this function returns NULL.
Usage notes
- For single-record inputs, STDDEV and STDDEV_SAMP both return NULL. This is different from the Oracle behavior, where STDDEV_SAMP returns NULL for a single record and STDDEV returns 0.
- When passed a VARCHAR expression, this function implicitly casts the input to floating point values. If the cast cannot be performed, an error is returned.
- When this function is called as a window function and the OVER clause contains an ORDER BY clause: The DISTINCT keyword is prohibited and results in a SQL compilation error.
- A window frame must be specified. If you do not specify a window frame, the following implied window frame is used: RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
- For more details about window frames, including syntax and examples, see Usage notes for window frames.
Aggregate function examples
The following example calculates the standard deviation for a small sample of integers: Note that the function STDDEV_SAMP returns the same result: The following example uses a small table named menu_items, which lists items for sale from a food truck. If you would like to create and load this table, see Create and load the menu_items table. To find the sample standard deviation for both the cost of goods sold (COGS) and the sale price for the Dessert rows, run this query:
Window function example
The following example also uses the menu_items table (see Create and load the menu_items table) but calls the STDDEV function as a window function. The window function partitions rows by the menu_category column. Therefore, the standard deviation is calculated once for each category, and that value is repeated in the result for each row in the group. In this example, the rows must be grouped by both the menu category and the cost of goods sold. The following output is a partial result set for this query (the first 15 rows):
Create and load the menu_items table
To create and insert rows into the menu_items table that is used in some function examples, run the following SQL commands. (This table contains 60 rows. It is based on, but not identical to, the menu table in the Tasty Bytes sample database.)