Group By
suggest changeSyntax
- SELECT expression1, expression2, … expression_n,
- aggregate_function (expression)
- FROM tables
- [WHERE conditions]
- 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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents