PostgreSQL Group By Clause allows you to arrange the rows of a query in groups. The groups are determined by the columns that you specify in the GROUP BY clause. Aggregate functions can be min,max,sum, avg, count.
PostgreSQL Group By Clause Syntax:
SELECT column-list FROM table_name WHERE [conditions ] GROUP BY column1, column2....columnN ORDER BY column1, column2....columnN
Order of Execution:
1. From clause
2. Where condtion
3. Group By
4. Having
5. Order By
PostgreSQL Group By Clause Examples:
Below table used for below Group by clause examples:
1. Calculate total salaries paid to each department in emp table.
select deptno, sum(sal) as total_sal_dept from emp group by deptno order by deptno;
2. Get count of each job types.
select job, count(job) from emp group by job;
3. Find the employees count in each department.
select deptno, count(deptno) from emp group by deptno order by deptno;