SQL Keyword - GROUP BY

GROUP BY Keyword

The Group By keyword is used to arrange identical data into groups using aggregate functions like COUNT, MAX, MIN, SUM, AVG.

We can group the data by using one or more columns.

Group By Clause is used with the Select query.

Group By Clause comes after the Where Clause but before the Order By Clause. 

Syntax

SELECT AGGREGATE_FUNCTION(<COLUMN_NAME>) FROM <TABLE_NAME> GROUP BY <COLUMN_NAME>;

Example 1

SELECT Emp_Dept,COUNT(Emp_Dept) 'No Of Employee' FROM Employee GROUP BY Emp_Dept;

Example 2

SELECT County, State, MIN(P_Age) As Min_Age,MAX(P_Age) As Max_Age FROM Persons
GROUP BY County, State;

Example 3

SELECT Emp_Dept ,COUNT(Emp_Dept) 'No Of Employee',SUM(Emp_Salary) 'Total Salary' FROM Employee GROUP BY Emp_Dept;

Summary

The Group By statement is used for organising similar data into groups.