Javatpoint Logo

91-9990449935

 0120-4256464

PostgreSQL GROUP BY Clause

The PostgreSQL GROUP BY clause is used to group together those rows in a table that have identical data. It is used with SELECT statement.

The GROUP BY clause collects data across multiple records and groups the result by one or more columns. It is also used to reduce the redundancy in the output.

Syntax:

Note: In case of GROUP BY multiple columns you make sure whatever column you are using to group, that column should be available in column-list.

See this example:

Let's take a table "EMPLOYEES" having the following data.

postgresql group by clause

Execute the following query:

Output:

postgresql group by clause

How it reduce redundancy:

See this example:

Let's insert some duplicate records in the "EMPLOYEES" table. Add the following data:

Output:

postgresql group by clause postgresql group by clause

Execute the following query to see the redundancy:

Output:

postgresql group by clause

Note:

In the above example you can see that the duplicate names are merged when we use GROUP BY "NAME". It specifies that GROUP BY reduces redundancy.