PostgreSQL GROUP BY Clause

Group by Clause is used to break the data into Groups.

Syntax:-
SELECT Column1, aggregate_function(Column2)
FROM TABLE
GROUP BY Column1; 

employees is the table, that i’m going to use. here making department_id columns records into groups.

pic1
  • select department_id, using count aggregate function on department_id making department_id in groups.
counting total department_id
  • using max aggregate function on salary for counting maximum salary by department_id .
max salary on department
  • if we use group by clause on column then we cant use non aggregate columns on query.
non-aggregate column “empoyee_id”
  • After including employee_id in Group , No Error Found.
we can make group by multiple columns

Happy Queries, Thanks.

Post Author: adama