GROUP BY clause is used to associate an aggregate function with groups of rows.
The SQL GROUP BY clause is used along with the SQL aggregate functions like SUM, COUNT, MAX, MIN, and AVG to provide means of grouping the result by refer to certain database table column.

The SQL syntax for GROUP BY is :

SELECT AGGREGATE FUNCTION ( [COLUMN NAME] )
FROM
[TABLE NAME] GROUP BY[COLUMN NAME]


EXAMPLE (WITH SUM) :

We can use GROUP BY clause to calculate sum of the scores by Department.

Table GameScores

PlayerNameDepartmentScores
JasonIT3000
IreneIT1500
JaneMarketing1000
DavidMarketing2500
PaulHR2000
JamesHR2000

SQL statement :

SELECT Department, SUM(Scores)
FROM
GameScores
GROUP BY Department

Result:

DepartmentSUM(Scores)
HR4000
IT4500
Marketing3500


EXAMPLE (WITH COUNT) :

We can use GROUP BY clause to calculate the number of contestants by Department.

SQL statement :

SELECT Department, COUNT(*)
FROM GameScores
GROUP BY Department

Result:

DepartmentCOUNT(*)
HR2
IT2
Marketing2