SQL GROUP BY Clause
The SQL GROUP BY Clause is used along with the group functions to retrieve data grouped according to one or more columns.
For Example: If you want to know the total amount of salary spent on each department, the query would be:
SELECT dept, SUM (salary)
FROM employee
GROUP BY dept;
FROM employee
GROUP BY dept;
The output would be like:
dept
-------------
Electrical
Electronics
Aeronautics
InfoTech
-------------
Electrical
Electronics
Aeronautics
InfoTech
salary
-------------
25000
55000
35000
30000
-------------
25000
55000
35000
30000
SELECT location, dept, SUM (salary)
FROM employee
FROM employee
GROUP BY location, dept;
FROM employee
FROM employee
GROUP BY location, dept;
The output would be like:
location
-------------
Bangalore
Bangalore
Mysore
Mangalore
-------------
Bangalore
Bangalore
Mysore
Mangalore
dept
-------------
Electrical
Electronics
Aeronautics
InfoTech
-------------
Electrical
Electronics
Aeronautics
InfoTech
salary
-------------
25000
55000
35000
30000
-------------
25000
55000
35000
30000