STDEV(expression) returns the sample standard deviation of the non-null values, dividing by n - 1. In SQL that is stddev_samp(Sales). A group with a single value has no sample standard deviation and returns null.
Updated · Checked on the xmetric engine
What is the syntax?
STDEV(expression)
expression
a numeric field or row-level calculation.
Returns a real number: the sample standard deviation, dividing by n - 1. Null when the group has no values or only one.
What does it return, and what is the SQL?
Each example runs on the same 12 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.
What is the standard deviation of sales in each region?
Tableau
STDEV([Sales])
SQL (DuckDB)
SELECT Region, stddev_samp(Sales) AS result
FROM input
GROUPBY Region
ORDERBY Region
East's four sales values give 110.87. South has one value: null, since a sample needs two.
Region
Result
East
111
North
null
South
null
West
108
✓ The Tableau formula and the SQL return the same 4 rows.
Where does a straight translation go wrong?
Common mistake
Why does stddev_pop give a smaller standard deviation?
STDEV is the sample standard deviation: it divides by n - 1. stddev_pop divides by n, the population version (STDEVP). East comes out at 110.87 against 96.01 for the population version.
Tableau
STDEV([Sales])
Tempting SQL
SELECT Region, stddev_pop(Sales) AS result
FROM input
GROUPBY Region
ORDERBY Region
SQL that matches
SELECT Region, stddev_samp(Sales) AS result
FROM input
GROUPBY Region
ORDERBY Region
Region
Tableau
Tempting SQL
SQL that matches
East
111
96.01
111
North
null
null
null
South
null
0
null
West
108
93.54
108
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
What does STDEV return for an all-null group and a single value?
North has two order lines and no sales value on either. Per product, Lamp has one line with a sale (row 4); the North lines have no product and no sales: null for both. Each result column is one mark, by Region.
Case
Tableau, then SQL (DuckDB)
East
North
South
West
all-null group
STDEV([Sales])Region, stddev_samp(Sales)
111
null
null
108
one value
STDEV([Sales])Product, stddev_samp(Sales)
11.55
96.18
null
null
✓ On every row, each Tableau formula and its SQL return the same value.
How do I write it in Snowflake or BigQuery?
The first example, written for each warehouse from its documentation. These are not run here; DuckDB is.
Snowflake
SELECT Region, STDDEV_SAMP(Sales) AS result
FROM input
GROUPBY Region
ORDERBY Region