Grouping retrieved data with the GROUP BY clause, in combination with the SELECT statement, is used to group rows in sub-groups by one or more values of columns of expression.

The following example shows a basic syntax for GROUP BY clause:


SELECT c1, c2, FROM table WHERE where_conditions GROUP BY c1, c2, ...

Or in a real world example when we want to list all the employees grouped by their salary:

SELECT emp FROM Emp GROUP BY salary;

And with the WHERE clause:

SELECT emp FROM Emp WHERE salary > 60000 GROUP BY salary DESC;

NOTE: The GROUP BY clause must appear after the FROM or WHERE clauses.