Functions

MySQL provides built-in functions that perform special operations.

Aggregate Functions

Aggregate functions operate on a set of data. These are usually used to perform some action on a complete set of returned rows. For example, SELECT AVG(height) FROM kids would return the average of all the values of the height field in the kids table. AVG( ), COUNT( ), and SUM( ) allow DISTINCT.

AVG(expression)

Returns the average value of the values in expression (e.g., SELECT AVG(score) FROM tests).

BIT_AND(expression)

Returns the bitwise AND aggregate of all the values in expression (e.g., SELECT BIT_AND(flags) FROM options). A bit will be set in the result if and only if the bit is set in every input field.

BIT_OR(expression)

Returns the bitwise OR aggregate of all the values in expression (e.g., SELECT BIT_OR(flags) FROM options). A bit is set in the result if it is set in at least one of the input fields.

BIT_XOR(expression)

Returns the bitwise XOR aggregate of all the values in expression with 64-bit precision.

COUNT(expression)

Returns the number of times expression was not null. COUNT(*) will return the number of rows with some data in the entire table (e.g., SELECT COUNT(*) FROM folders).

GROUP_CONCAT([DISTNCT] expression [ORDER BY {column | expression}] [SEPARATOR sep])

Provides a string that combines in order all the results marked by expression.

MAX(expression)

Returns the largest value in expression (e.g., SELECT MAX (elevation) FROM mountains).

MIN(expression)

Returns the smallest value ...

Get MySQL Pocket Reference, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.