Aggregate functions
COUNT, SUM, AVG, MIN, MAX, GROUP_CONCAT, the standard deviation and variance family, the BIT_ functions and JSON_ARRAYAGG, with GROUP BY and WITH ROLLUP.
An aggregate function computes one value from the rows of a group. Without GROUP BY the group is the whole result. NULL values are ignored by every aggregate except COUNT(*) and JSON_ARRAYAGG. Put DISTINCT inside COUNT, SUM, AVG, MIN, MAX and GROUP_CONCAT to use each different value once. Only GROUP_CONCAT takes several expressions; for the others a list is refused as not supported.
An aggregate followed by OVER (...) is a window function: see Window functions.
The examples on this page use this table:
CREATE TABLE t (g INT, x INT, y DOUBLE, s VARCHAR(5));
INSERT INTO t VALUES (1, 1, 1.5, 'a'), (1, 3, 2.5, 'b'), (1, 5, 4.5, NULL), (2, 7, 8.0, 'c'), (3, NULL, NULL, NULL);GROUP BY and WITH ROLLUP#
GROUP BY a, b WITH ROLLUP adds, after the last group of each leading part of the key, a row with the totals, the columns after that part NULL; the last row is the grand total. The groups are put in the order of their keys, NULL first.
SELECT y, SUM(n) FROM s GROUP BY y WITH ROLLUP;Dialect differences (checked against MySQL and MariaDB): MariaDB refuses ORDER BY together with WITH ROLLUP (error 1221, CUBE/ROLLUP cannot be combined with ORDER BY), MySQL allows it. For an input without rows MySQL answers the totals row, MariaDB answers nothing. GROUPING() and CUBE are not supported yet.
ONLY_FULL_GROUP_BY#
With ONLY_FULL_GROUP_BY in the sql_mode (the MySQL default; MariaDB's default has it off), a column of the select list, HAVING or ORDER BY that is not inside an aggregate must be a grouping column or depend on one, or the statement is refused as MySQL refuses it.
Summary of functions#
| Function | What it does | Dialect |
|---|---|---|
AVG | Returns the average of the values of expr, ignoring NULL. | MySQL and MariaDB |
BIT_AND | Returns the bitwise AND of all values of expr as an unsigned 64-bit number. | MySQL and MariaDB |
BIT_OR | Returns the bitwise OR of all values of expr as an unsigned 64-bit number. | MySQL and MariaDB |
BIT_XOR | Returns the bitwise XOR of all values of expr as an unsigned 64-bit number. | MySQL and MariaDB |
COUNT | COUNT(*) returns the number of rows. | MySQL and MariaDB |
GROUP_CONCAT | Joins the non-NULL values of each group into one string, separated by SEPARATOR (a comma by default), in the ORDER BY order. | MySQL and MariaDB |
JSON_ARRAYAGG | Returns the values of the group as one JSON array, NULL values included. | MySQL and MariaDB |
MAX | Returns the largest value of expr; for text by the case-insensitive order. | MySQL and MariaDB |
MIN | Returns the smallest value of expr. | MySQL and MariaDB |
STD | Population standard deviation. | MySQL and MariaDB |
STDDEV | Population standard deviation. | MySQL and MariaDB |
STDDEV_POP | Returns the population standard deviation of expr, ignoring NULL. | MySQL and MariaDB |
STDDEV_SAMP | Returns the sample standard deviation of expr (divided by n-1). | MySQL and MariaDB |
SUM | Returns the sum of the values of expr, ignoring NULL. | MySQL and MariaDB |
VARIANCE | Population variance. | MySQL and MariaDB |
VAR_POP | Returns the population variance of expr, ignoring NULL. | MySQL and MariaDB |
VAR_SAMP | Returns the sample variance of expr (divided by n-1). | MySQL and MariaDB |
AVG#
AVG([DISTINCT] expr)Returns the average of the values of expr, ignoring NULL. The result has four more decimals than expr. NULL if there are no values.
Example:
SELECT AVG(x) FROM t;Result: 4.0000 for the values 1, 3, 5, 7 and NULL (the table above)
BIT_AND#
BIT_AND(expr)Returns the bitwise AND of all values of expr as an unsigned 64-bit number. An empty group gives 18446744073709551615.
Example:
SELECT BIT_AND(x) FROM t WHERE g = 1;Result: 1 for the values 1, 3, 5
BIT_OR#
BIT_OR(expr)Returns the bitwise OR of all values of expr as an unsigned 64-bit number. An empty group gives 0.
Example:
SELECT BIT_OR(x) FROM t WHERE g = 1;Result: 7 for the values 1, 3, 5
BIT_XOR#
BIT_XOR(expr)Returns the bitwise XOR of all values of expr as an unsigned 64-bit number. An empty group gives 0.
Example:
SELECT BIT_XOR(x) FROM t WHERE g = 1;Result: 7 for the values 1, 3, 5
COUNT#
COUNT(*)
COUNT([DISTINCT] expr)COUNT(*) returns the number of rows. COUNT(expr) returns the number of rows where expr is not NULL; with DISTINCT the number of different values.
Example:
SELECT COUNT(*), COUNT(x), COUNT(DISTINCT g) FROM t;Result: 5, 4, 3
GROUP_CONCAT#
GROUP_CONCAT([DISTINCT] expr [, expr ...]
[ORDER BY expr [ASC | DESC] [, ...]]
[LIMIT [offset,] count]
[SEPARATOR str])Joins the non-NULL values of each group into one string, separated by SEPARATOR (a comma by default), in the ORDER BY order. The result is cut to group_concat_max_len bytes (1024 by default). NULL for a group without values. LIMIT is read in the MariaDB dialect only.
Example:
SELECT GROUP_CONCAT(s ORDER BY s DESC SEPARATOR '-') FROM t;Result: c-b-a
JSON_ARRAYAGG#
JSON_ARRAYAGG(expr)Returns the values of the group as one JSON array, NULL values included. NULL for an empty group. MySQL writes [1, 3, 5], MariaDB [1,3,5].
Example:
SELECT JSON_ARRAYAGG(x) FROM t WHERE g = 1;Result: [1, 3, 5] (MySQL), [1,3,5] (MariaDB)
MAX#
MAX([DISTINCT] expr)Returns the largest value of expr; for text by the case-insensitive order. NULL if there are no values.
Example:
SELECT MAX(x) FROM t;Result: 7
MIN#
MIN([DISTINCT] expr)Returns the smallest value of expr. NULL if there are no values.
Example:
SELECT MIN(x) FROM t;Result: 1
STD#
STD(expr)Population standard deviation. Synonym of STDDEV_POP.
Example:
SELECT STD(x) FROM t WHERE g = 2;Result: 0
STDDEV#
STDDEV(expr)Population standard deviation. Synonym of STDDEV_POP.
Example:
SELECT STDDEV(x) FROM t WHERE g = 2;Result: 0
STDDEV_POP#
STDDEV_POP(expr)Returns the population standard deviation of expr, ignoring NULL. NULL if there are no values. MySQL returns a double; MariaDB returns a decimal with four more places than a whole-number or decimal argument has.
Example:
SELECT STDDEV_POP(x) FROM t WHERE g = 2;Result: 0
STDDEV_SAMP#
STDDEV_SAMP(expr)Returns the sample standard deviation of expr (divided by n-1). NULL if there are fewer than two values.
Example:
SELECT STDDEV_SAMP(x) FROM t WHERE g = 2;Result: NULL
SUM#
SUM([DISTINCT] expr)Returns the sum of the values of expr, ignoring NULL. NULL if there are no values. The sum of whole numbers is a whole number; any other sum is a decimal with the scale of the argument.
Example:
SELECT SUM(x) FROM t;Result: 16
VARIANCE#
VARIANCE(expr)Population variance. Synonym of VAR_POP.
Example:
SELECT VARIANCE(x) FROM t WHERE g = 2;Result: 0
VAR_POP#
VAR_POP(expr)Returns the population variance of expr, ignoring NULL. NULL if there are no values.
Example:
SELECT VAR_POP(x) FROM t WHERE g = 2;Result: 0
VAR_SAMP#
VAR_SAMP(expr)Returns the sample variance of expr (divided by n-1). NULL if there are fewer than two values.
Example:
SELECT VAR_SAMP(x) FROM t WHERE g = 2;Result: NULL