SQL GROUP BY
statement is used to arrange identical data into groups with the help of aggregate functions such as COUNT, SUM, MAX, MIN, and AVG. This statement is often used with aggregate functions to perform calculations on each group of data.
SELECT column_name(s), aggregate_function(column_name)
FROM table_name
WHERE condition
GROUP BY column_name(s);
Example
Consider a table named Employees
with the following data:
EmployeeID | Name | Department | Salary |
1 | John | HR | 5000 |
2 | Jane | IT | 6000 |
3 | Bill | IT | 7000 |
4 | Mary | HR | 4000 |
5 | Anna | IT | 8000 |
Objective: Find the average salary for each department.
SQL Query using Group By
SELECT Department, AVG(Salary) AS AverageSalary
FROM Employees
GROUP BY Department;
Explanation of Group By clause
- SELECT Department, AVG(Salary) AS AverageSalary: This part of the query selects the
Department
column and calculates the average salary for each department, renaming the result asAverageSalary
. - FROM Employees: This specifies the table from which to retrieve the data.
- GROUP BY Department: This groups the result set by the
Department
column.
Result
The query will return a result set like this:
Department | AverageSalary |
HR | 4500 |
IT | 7000 |
- HR: The average salary is calculated as (5000 + 4000) / 2 = 4500.
- IT: The average salary is calculated as (6000 + 7000 + 8000) / 3 = 7000.
In summary, the GROUP BY
statement is useful for summarizing data by grouping rows that have the same values in specified columns, and then performing aggregate calculations on each group.