STDDEV

functionsupportedCore SQL functions

docs.snowflake.com ↗

Data metric functionsAggregate functionsWindow function syntax and usage

Returns 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

NameRequiredDescription
expr1requiredAn expression that evaluates to a numeric value. This is the expression on which the standard deviation is calculated.
expr2requiredThis is the optional expression to partition by.
expr3requiredThis 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

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.)