PostgreSQL GROUP BY Clause

The PostgreSQL Group By statement is used to group identical values ​​into summary rows such as "Search the number of users in each state". The GROUP BY statement is often used with aggregate functions and follows the WHERE clause in a select statement.For each group, an aggregate function like sum(), count () etc. can be applied.


GROUP BY Syntax :

The syntax of the GROUP BY statement is −

SELECT col_name, group_func(col_name)
from employees
group by col_name, [col_name]

GROUP BY Example :

Consider the STUDENT table is having the following records

ID NAME AGE ADDRESS
1 Jugal 21 Indore
2 Kartik 19 Delhi
3 Ashish 18 Udaipur
4 Prateek 19 Jaipur
5 Pranjal 20 Mumbai
6 Vivek 24 Mumbai
7 Divesh 22 Udaipur
8 Ayush 21 Indore

TEST CASE 1 :- The following code is an example, which would fetch the all record from STUDENT table where AGE is greater equal to 20.

lfcdb=# SELECT ADDRESS, AVG(AGE) FROM STUDENT GROUP BY ADDRESS ORDER BY ADDRESS;

The result for the respective query is as follows −

ADDRESS AGE
Delhi 19
Indore 21
Jaipur 19
Udaipur 20
Mumbai 22







Visit :


Discussion



* You must be logged in to add comment.