The GROUP BY clause divides the rows returned from the SELECT statement into groups. For each group, you can apply an aggregate function e.g., SUM() to calculate the sum of items or COUNT() to get the number of items in the groups.
The following statement illustrates the basic syntax of the GROUP BY clause:
```
SELECT
column_1,
column_2,
...,
aggregate_function(column_3)
FROM
table_name
GROUP BY
column_1,
column_2,
...;
```
In this syntax:
* First, select the columns that you want to group e.g., column1 and column2, and column that you want to apply an aggregate function (column3).
* Second, list the columns that you want to group in the GROUP BY clause.
The statement clause divides the rows by the values of the columns specified in the GROUP BY clause and calculates a value for each group.
It’s possible to use other clauses of the SELECT statement with the GROUP BY clause.
SQL evaluates the GROUP BY clause after the FROM and WHERE clauses and before the HAVING SELECT, DISTINCT, ORDER BY and LIMIT clauses.
```
SELECT
customer_id
FROM
payment
GROUP BY
customer_id;
```