SQL GROUP BY
So far, we have learned how to:
- Filter records using WHERE
- Sort data using ORDER BY
- Summarize data using Aggregate Functions like COUNT, SUM, AVG
But there is a common real-world requirement:
“Show summary category-wise, not for the entire table.”
Examples
- Total salary per department
- Number of employees in each city
- Average marks per subject
If we use aggregate functions without grouping, SQL returns only one result for the whole table. To generate summaries group-wise, SQL provides the GROUP BY clause.
What Is the GROUP BY Clause?
The GROUP BY clause is used to group rows that have the same values in one or more columns and then apply aggregate functions on each group.
GROUP BY works as a bridge between raw data and summarized reports.
How GROUP BY Works
1. SQL scans all rows in the table
2. Rows with the same group value are placed together
3. Aggregate functions are applied to each group separately
4. One result row is returned per group
Syntax
SELECT column_name, aggregate_function(column_name)FROM table_nameGROUP BY column_name;Example: employees Table
| id | name | salary | department |
|---|---|---|---|
| 1 | Amit | 60000 | IT |
| 2 | Neha | 75000 | HR |
| 3 | Rahul | 50000 | IT |
| 4 | Priya | 90000 | Finance |
| 5 | Karan | 45000 | HR |
Example - 1: Total Salary per Department
Problem: Find the total salary paid in each department.
SELECT department, SUM(salary) AS total_salaryFROM employeesGROUP BY department;Output
department total_salary IT 110000 HR 120000 Finance 90000
Each department is treated as one group.
Why GROUP BY is Mandatory Here
If we write
SELECT department, SUM(salary)FROM employees;SQL will throw an error.
Reason
When using aggregate functions, all non aggregated columns must appear in GROUP BY.
Example - 2: Count Employees per Department
SELECT department, COUNT(*) AS total_employeesFROM employeesGROUP BY department;Shows how many employees work in each department.
GROUP BY with WHERE Clause
Execution Order
- WHERE - filters rows
- GROUP BY - groups filtered rows
- Aggregate functions - applied to each group
Example: Count IT Employees by Department
SELECT department, COUNT(*)FROM employeesWHERE department = 'IT'GROUP BY department;Filtering happens before grouping.
GROUP BY with Multiple Columns
Why Multiple Columns Are Used
Sometimes grouping by one column is not enough.
Example
- Department and city
- Year and month
Syntax
SELECT column1, column2, aggregate_function(column)FROM table_nameGROUP BY column1, column2;Example: Employees per Department and City
SELECT department, city, COUNT(*)FROM employeesGROUP BY department, city;Each unique combination becomes a separate group.
Important Points About GROUP BY
- GROUP BY is used with aggregate functions
- One result row is returned per group
- All non-aggregated columns must be in GROUP BY
- WHERE filters rows before grouping
- HAVING filters groups after grouping
Two Minute Drill
- GROUP BY groups similar values
- Used with aggregate functions
- One row per group is returned
- WHERE filters rows first
- HAVING filters grouped results
- Non aggregated columns must be in GROUP BY