STDDEV()

USAGE

STDDEV(number)

Argument Name

Description

number

The number to be aggregated

DETAILS

See STDDEV_POP().

SYNONYMS

SCHEMA

PARAMETERS

SKYSQL

PRIVILEGES

EXAMPLES

Example Schema and Data

All of the examples are based on the inventory table:

CREATE TABLE inventory (
  item VARCHAR(20),
  category VARCHAR(20),
  count INT
);
INSERT INTO inventory VALUES
  ('Foo','in stock', 42), ('Foo','back order', 50),
  ('Bar','in stock', 10), ('Bar','back order', 60),
  ('Baz','in stock', 99), ('Baz','back order', 10),
  ('FooBar','in stock', 10), ('FooBar','back order', NULL),
  ('BarBaz','in stock', NULL), ('BarBaz','back order', NULL);

The values to be computed:

SELECT item, count
 FROM inventory;
+--------+-------+
| item   | count |
+--------+-------+
| Foo    |    42 |
| Foo    |    50 |
| Bar    |    10 |
| Bar    |    60 |
| Baz    |    99 |
| Baz    |    10 |
| FooBar |    10 |
| FooBar |  NULL |
| BarBaz |  NULL |
| BarBaz |  NULL |
+--------+-------+

Default Behavior

In this example, all row values are used to compute the standard deviation (ignoring NULL values):

SELECT STDDEV(count)
 FROM inventory;
+-----------------+
| STDDEV(count)   |
+-----------------+
| 30.902744082686 |
+-----------------+

With GROUP BY

Using STDDEV() with GROUP BY creates a separate population standard deviation for each group. In this example, GROUP BY category creates a separate standard deviation for each unique category:

SELECT category, STDDEV(count)
 FROM inventory
 GROUP BY category
 ORDER BY category;
+------------+-----------------+
| category   | STDDEV(count)   |
+------------+-----------------+
| back order |  21.60246899477 |
| in stock   | 36.348143006211 |
+------------+-----------------+

With NULL Values

When STDDEV() acts on NULL values, those values are not included when computing the standard deviation. In this example, some rows in the data set contain NULL values in the count column:

SELECT *
 FROM inventory
 WHERE item = 'FooBar'
 ORDER BY count;
+--------+------------+-------+
| item   | category   | count |
+--------+------------+-------+
| FooBar | back order |  NULL |
| FooBar | in stock   |    10 |
+--------+------------+-------+

The following example computes a standard deviation of the count column for the FooBar item:

SELECT STDDEV(count)
 FROM inventory
 WHERE item = 'FooBar';
+---------------+
| STDDEV(count) |
+---------------+
|             0 |
+---------------+

With Only NULL Values

When STDDEV() is acting only on NULL values, STDDEV() returns NULL. In this example, all rows in the data set contain NULL values in the count column:

SELECT *
 FROM inventory
 WHERE item = 'BarBaz'
 ORDER BY count;
+--------+------------+-------+
| item   | category   | count |
+--------+------------+-------+
| BarBaz | in stock   |  NULL |
| BarBaz | back order |  NULL |
+--------+------------+-------+

The following example computes a standard deviation of the count column for the BarBaz item:

SELECT STDDEV(count)
 FROM inventory
 WHERE item = 'BarBaz';
+---------------+
| STDDEV(count) |
+---------------+
|          NULL |
+---------------+

ERROR HANDLING

FEATURE INTERACTION

RESPONSES

DIAGNOSIS

ISO 9075:2016

CHANGE HISTORY

Release Series

History

23.09

  • Present starting in MariaDB Xpand 23.09.1.

6.1

  • Present starting in MariaDB Xpand 6.1.0.

6.0

  • Present starting in MariaDB Xpand 6.0.3.

5.3

  • Present starting in MariaDB Xpand 5.3.13.

Release Series

History

6.0

  • Present starting in MariaDB Xpand 6.0.3.

5.3

  • Present starting in MariaDB Xpand 5.3.13.

Release Series

History

6.1

  • Present starting in MariaDB Xpand 6.1.0.

EXTERNAL REFERENCES