Loading
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_name
GROUP BY column_name;


Example: employees Table

idnamesalarydepartment
1Amit60000IT
2Neha75000HR
3Rahul50000IT
4Priya90000Finance
5Karan45000HR


Example - 1: Total Salary per Department

Problem: Find the total salary paid in each department.

SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department;


Output

departmenttotal_salary
IT110000
HR120000
Finance90000


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_employees
FROM employees
GROUP 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 employees
WHERE 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_name
GROUP BY column1, column2;


Example: Employees per Department and City

SELECT department, city, COUNT(*)
FROM employees
GROUP 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