SQL reference (MySQL and MariaDB dialects)

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:

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

SQL
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#

FunctionWhat it doesDialect
AVGReturns the average of the values of expr, ignoring NULL.MySQL and MariaDB
BIT_ANDReturns the bitwise AND of all values of expr as an unsigned 64-bit number.MySQL and MariaDB
BIT_ORReturns the bitwise OR of all values of expr as an unsigned 64-bit number.MySQL and MariaDB
BIT_XORReturns the bitwise XOR of all values of expr as an unsigned 64-bit number.MySQL and MariaDB
COUNTCOUNT(*) returns the number of rows.MySQL and MariaDB
GROUP_CONCATJoins 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_ARRAYAGGReturns the values of the group as one JSON array, NULL values included.MySQL and MariaDB
MAXReturns the largest value of expr; for text by the case-insensitive order.MySQL and MariaDB
MINReturns the smallest value of expr.MySQL and MariaDB
STDPopulation standard deviation.MySQL and MariaDB
STDDEVPopulation standard deviation.MySQL and MariaDB
STDDEV_POPReturns the population standard deviation of expr, ignoring NULL.MySQL and MariaDB
STDDEV_SAMPReturns the sample standard deviation of expr (divided by n-1).MySQL and MariaDB
SUMReturns the sum of the values of expr, ignoring NULL.MySQL and MariaDB
VARIANCEPopulation variance.MySQL and MariaDB
VAR_POPReturns the population variance of expr, ignoring NULL.MySQL and MariaDB
VAR_SAMPReturns the sample variance of expr (divided by n-1).MySQL and MariaDB

AVG#

SQL
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:

SQL
SELECT AVG(x) FROM t;

Result: 4.0000 for the values 1, 3, 5, 7 and NULL (the table above)

BIT_AND#

SQL
BIT_AND(expr)

Returns the bitwise AND of all values of expr as an unsigned 64-bit number. An empty group gives 18446744073709551615.

Example:

SQL
SELECT BIT_AND(x) FROM t WHERE g = 1;

Result: 1 for the values 1, 3, 5

BIT_OR#

SQL
BIT_OR(expr)

Returns the bitwise OR of all values of expr as an unsigned 64-bit number. An empty group gives 0.

Example:

SQL
SELECT BIT_OR(x) FROM t WHERE g = 1;

Result: 7 for the values 1, 3, 5

BIT_XOR#

SQL
BIT_XOR(expr)

Returns the bitwise XOR of all values of expr as an unsigned 64-bit number. An empty group gives 0.

Example:

SQL
SELECT BIT_XOR(x) FROM t WHERE g = 1;

Result: 7 for the values 1, 3, 5

COUNT#

SQL
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:

SQL
SELECT COUNT(*), COUNT(x), COUNT(DISTINCT g) FROM t;

Result: 5, 4, 3

GROUP_CONCAT#

SQL
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:

SQL
SELECT GROUP_CONCAT(s ORDER BY s DESC SEPARATOR '-') FROM t;

Result: c-b-a

JSON_ARRAYAGG#

SQL
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:

SQL
SELECT JSON_ARRAYAGG(x) FROM t WHERE g = 1;

Result: [1, 3, 5] (MySQL), [1,3,5] (MariaDB)

MAX#

SQL
MAX([DISTINCT] expr)

Returns the largest value of expr; for text by the case-insensitive order. NULL if there are no values.

Example:

SQL
SELECT MAX(x) FROM t;

Result: 7

MIN#

SQL
MIN([DISTINCT] expr)

Returns the smallest value of expr. NULL if there are no values.

Example:

SQL
SELECT MIN(x) FROM t;

Result: 1

STD#

SQL
STD(expr)

Population standard deviation. Synonym of STDDEV_POP.

Example:

SQL
SELECT STD(x) FROM t WHERE g = 2;

Result: 0

STDDEV#

SQL
STDDEV(expr)

Population standard deviation. Synonym of STDDEV_POP.

Example:

SQL
SELECT STDDEV(x) FROM t WHERE g = 2;

Result: 0

STDDEV_POP#

SQL
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:

SQL
SELECT STDDEV_POP(x) FROM t WHERE g = 2;

Result: 0

STDDEV_SAMP#

SQL
STDDEV_SAMP(expr)

Returns the sample standard deviation of expr (divided by n-1). NULL if there are fewer than two values.

Example:

SQL
SELECT STDDEV_SAMP(x) FROM t WHERE g = 2;

Result: NULL

SUM#

SQL
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:

SQL
SELECT SUM(x) FROM t;

Result: 16

VARIANCE#

SQL
VARIANCE(expr)

Population variance. Synonym of VAR_POP.

Example:

SQL
SELECT VARIANCE(x) FROM t WHERE g = 2;

Result: 0

VAR_POP#

SQL
VAR_POP(expr)

Returns the population variance of expr, ignoring NULL. NULL if there are no values.

Example:

SQL
SELECT VAR_POP(x) FROM t WHERE g = 2;

Result: 0

VAR_SAMP#

SQL
VAR_SAMP(expr)

Returns the sample variance of expr (divided by n-1). NULL if there are fewer than two values.

Example:

SQL
SELECT VAR_SAMP(x) FROM t WHERE g = 2;

Result: NULL

See also#