Stats

80 Contributors: 7 Tuesday, May 2, 2017
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap

Group By

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

ParameterDETAILS
expression1, expression2, ... expression_nThe expressions that are not encapsulated within an aggregate function and must be included in the GROUP BY clause.
aggregate_functionA function such as SUM, COUNT, MIN, MAX, or AVG functions.
tableshe tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
WHERE conditionsOptional. The conditions that must be met for the records to be selected.

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 is governed in part by the value of the ONLY_FULL_GROUP_BY variable. When this is enabled, SELECT statements that group by any column not in the output return an error. (This is the default as of 5.7.5.) Both setting and not setting this variable can cause problems for naive users or users accustomed to other DBMSs.

Related Examples