GROUP BY clause

The GROUP BY clause specifies the names of one or more fields by which the returned values should be grouped. This clause is used to return a set of aggregate values. It has the following format:

Copy
GROUP BY columns

The scope of the GROUP BY clause is the table expression in the FROM clause. As a result, the column expressions specified by columns must be from the tables specified in the FROM clause. A column expression can be one or more field names of the database table separated by commas.

Example

Sum the salaries in each department.

Copy
SELECT dept_id, SUM (salary) FROM emp GROUP BY dept_id

This statement returns one row for each distinct department ID. Each row contains the department ID and the sum of the salaries of the employees in the department.