Skip to content

GROUP BY

The GROUP BY command is used in SQL to group multiple results and use a totals function on a group of results.

sql
SELECT
   column_1,
   column_2,
   ...,
   aggregate_function(column_3)
FROM
   table_name
GROUP BY
   column_1,
   column_2,
   ...;

INFO

This command must always be used after the WHERE command and before the HAVING command.

Examples:

sql
SELECT client, SUM(rate)
FROM purchase
GROUP BY client

There are several functions that can be used to manipulate multiple records, these are statistical aggregation functions, the main ones are as follows:

  • AVG() to calculate the average of a set of values.
  • COUNT() to count the number of rows concerned.
  • MAX() to retrieve the highest value.
  • MIN() to retrieve the smallest value.
  • SUM() to calculate the sum of several lines.

References