What is the use of the GROUP BY clause?
The GROUP BY
clause in SQL is used to group rows that have the same values in specified columns into summary rows (like “total” or “average”). It is typically used with aggregate functions (e.g., COUNT()
, SUM()
, AVG()
, MAX()
, MIN()
) to perform calculations on each group of rows.
Key Uses of the GROUP BY
Clause:
- Grouping Rows: It groups the result set by one or more columns, allowing you to perform aggregate calculations on each group.
- Aggregating Data: Used with aggregate functions to calculate sums, averages, counts, etc., for each group.
Basic Syntax:
SELECT column1, column2, AGGREGATE_FUNCTION(column3)
FROM table_name
GROUP BY column1, column2;
Explanation:
column1, column2
: The columns you want to group by.AGGREGATE_FUNCTION
: The function that performs an aggregation, likeSUM()
,AVG()
, etc.table_name
: The name of the table you’re querying.
Examples:
- Grouping by a Single Column:
Suppose you have anemployees
table, and you want to find out how many employees are in each department.
SELECT department, COUNT(*) AS num_employees
FROM employees
GROUP BY department;
Result:
This will return the count of employees grouped by their respective departments.
department | num_employees |
---|---|
HR | 5 |
IT | 10 |
Marketing | 3 |
- Grouping by Multiple Columns:
You can also group by multiple columns to get more granular results. For example, grouping bydepartment
andjob_title
.
SELECT department, job_title, COUNT(*) AS num_employees
FROM employees
GROUP BY department, job_title;
Result:
This will show how many employees are in each department, broken down by job title.
department | job_title | num_employees |
---|---|---|
HR | Manager | 2 |
IT | Developer | 5 |
IT | Analyst | 3 |
Marketing | Manager | 1 |
- Using Aggregate Functions:
TheGROUP BY
clause is often used in combination with aggregate functions. For example, calculating the average salary by department:
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;
Result:
This will return the average salary of employees in each department.
department | avg_salary |
---|---|
HR | 50000 |
IT | 70000 |
Marketing | 45000 |
- Using
HAVING
withGROUP BY
:
You can use theHAVING
clause to filter groups after they’ve been created byGROUP BY
. UnlikeWHERE
(which filters rows before grouping),HAVING
filters groups after the aggregation.
Example to find departments with more than 5 employees:
SELECT department, COUNT(*) AS num_employees
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;
Result:
This will return only departments that have more than 5 employees.
department | num_employees |
---|---|
IT | 10 |
Summary of Important Points:
GROUP BY
groups rows by one or more columns.- It’s typically used with aggregate functions (e.g.,
COUNT()
,SUM()
,AVG()
) to summarize data. HAVING
is used to filter the result of groups (unlikeWHERE
, which filters rows before grouping).- You can group by multiple columns for more specific aggregation.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.