In Postgres, the GROUP BY clause is utilized to group the table’s data based on single or multiple columns. Grouping the table’s data offers various features like removing redundancy, improving code readability, helping in performing analytics, and so on. By grouping multiple columns, we can calculate different statistics for a group of records.
This write-up will illustrate the working of the GROUP BY clause on multiple columns in Postgres.
How to Use GROUP BY Clause on Two or More Columns in PostgreSQL?
In PostgreSQL, multiple columns can be used with the GROUP BY clause. It helps us aggregate/collect data into groups. You must specify a comma between the columns to be grouped when using the GROUP BY clause on multiple columns:
SELECT "col_1", "col_2", "col_N" FROM "tab_name" WHERE condition GROUP BY "col_1", "col_2", "col_N" ORDER BY "col_name";
The columns defined in the SELECT query must be utilized in the GROUP BY clause, otherwise, you can face unwanted circumstances.
Sample Table
We will utilize the “emp_bio” table in the upcoming examples, whose data is shown in the following snippet:
SELECT * FROM emp_bio;
Let’s head toward the practical examples.
Example 1: Group by a Single Column
Assume that we have to group the employee’s data with respect to their joining date. To do this, we will utilize the COUNT() function and the GROUP BY clause as follows:
SELECT joining_date, COUNT(e_id) AS total_employees FROM emp_bio GROUP BY joining_date;
The output shows that three employees joined in 2021 while the rest of two joined in 2022.
Example 2: Group by Multiple Columns
Now let’s suppose we have to group the employee’s data based on their date of joining and salaries. For this purpose, we need to specify the “joining_date” and “emp_sal” columns in the GROUP BY clause as follows:
SELECT joining_date, emp_sal FROM emp_bio GROUP BY joining_date, emp_sal;
The output shows that the employees have been grouped based on the selected columns.
Conclusion
In PostgreSQL, multiple columns can be used with the GROUP BY clause. You must specify a comma between the columns to be grouped when using the GROUP BY clause on multiple columns. The columns defined in the SELECT query must be utilized in the GROUP BY clause, otherwise, you can face unwanted circumstances. This post has explained the use of the GROUP BY clause on two or more columns.