MySQL Group By

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

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.



Got any MySQL Question?