Using the GROUP BY clause

The GROUP BY clause enables you to establish data groups based on columns. The grouping criterion is defined by the GROUP BY clause, which is followed by the WHERE clause in the SQL execution path. Following this execution path, the result set rows are grouped based on like values of grouping columns and the WHERE clause restricts the entries in each group.

Note

All columns that are used besides the aggregate functions must be included in the GROUP BY clause. The GROUP BY clause does not support the use of column aliases; you must use the actual column names. The GROUP BY columns may or may not appear in the SELECT list. The GROUP BY clause can only be used with aggregate functions such as  SUM , AVG , COUNT , ...

Get PostgreSQL Development Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.