Group By

suggest change

Syntax

  1. SELECT expression1, expression2, … expression_n,
  2. aggregate_function (expression)
  3. FROM tables
  4. [WHERE conditions]
  5. GROUP BY expression1, expression2, … expression_n;

Parameters

Parameter Details
expression1, expression2, … expression_n Expressions that are not inside an aggregate function. With ONLY_FULL_GROUP_BY, each one selected must appear in the GROUP BY.
aggregate_function A function such as SUM, COUNT, MIN, MAX, or AVG
tables The tables in the FROM clause. At least one is required
WHERE conditions Optional. Rows must match these conditions before grouping

Remarks

The MySQL GROUP BY clause is used in a SELECT statement to collect data across multiple records and group the results by one or more columns.

Its behavior depends on ONLY_FULL_GROUP_BY, which is on by default as of MySQL 5.7.5 (details). When it is on, selecting a column that is neither in the GROUP BY nor inside an aggregate function is an error. Grouping by a column you do not select is allowed. Turning the mode off restores MySQL’s old nonstandard behavior, which can return an arbitrary value from the group.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents