đ The GROUP BY clause in SQL is used to group rows that have the same values in one or more columns. It is commonly used with aggregate functions such as COUNT(), SUM(), AVG(), MIN(), and MAX() to generate summarized reports and analyze data.
đ What is GROUP BY?
The GROUP BY clause combines rows with identical values into groups. Aggregate functions are then applied to each group instead of the entire table.
Information
đ Basic Syntax
GROUP BY Syntax
SELECT column1,
aggregate_function(column2)
FROM table_name
WHERE condition
GROUP BY column1;đ Sample Table
Consider the following Students table:
| StudentID | Name | Department | City | Marks | FeesPaid |
|---|---|---|---|---|---|
| 101 | Alice | Computer Science | Chennai | 92 | 50000 |
| 102 | Bob | Mathematics | Coimbatore | 85 | 45000 |
| 103 | Charlie | Physics | Madurai | 78 | 40000 |
| 104 | David | Computer Science | Chennai | 95 | 50000 |
| 105 | Eva | Mathematics | Salem | 88 | 47000 |
1ī¸âŖ GROUP BY with COUNT()
Count the number of students in each department.
Count Students by Department
SELECT Department,
COUNT(*) AS TotalStudents
FROM Students
GROUP BY Department;Result:
| Department | TotalStudents |
|---|---|
| Computer Science | 2 |
| Mathematics | 2 |
| Physics | 1 |
2ī¸âŖ GROUP BY with SUM()
Calculate the total fees collected from each department.
Total Fees by Department
SELECT Department,
SUM(FeesPaid) AS TotalFees
FROM Students
GROUP BY Department;3ī¸âŖ GROUP BY with AVG()
Calculate the average marks for each department.
Average Marks by Department
SELECT Department,
AVG(Marks) AS AverageMarks
FROM Students
GROUP BY Department;4ī¸âŖ GROUP BY with MIN() and MAX()
Find the lowest and highest marks for each department.
Minimum and Maximum Marks
SELECT Department,
MIN(Marks) AS LowestMarks,
MAX(Marks) AS HighestMarks
FROM Students
GROUP BY Department;đ GROUP BY Multiple Columns
You can group records using more than one column.
Group by Department and City
SELECT Department,
City,
COUNT(*) AS TotalStudents
FROM Students
GROUP BY Department,
City;This query creates a separate group for every unique combination of Department and City.
đ GROUP BY with WHERE
The WHERE clause filters rows before grouping.
Filtered Groups
SELECT Department,
AVG(Marks) AS AverageMarks
FROM Students
WHERE Marks >= 80
GROUP BY Department;đ GROUP BY with HAVING
The HAVING clause filters groups after aggregate calculations.
Departments with More Than One Student
SELECT Department,
COUNT(*) AS TotalStudents
FROM Students
GROUP BY Department
HAVING COUNT(*) > 1;Tip
âĸ WHERE filters individual rows.
âĸ HAVING filters grouped results.
đ GROUP BY with ORDER BY
Use ORDER BY to sort grouped results.
Sort Grouped Results
SELECT Department,
AVG(Marks) AS AverageMarks
FROM Students
GROUP BY Department
ORDER BY AverageMarks DESC;đ GROUP BY with JOIN
The GROUP BY clause is commonly used with joins to summarize data across multiple tables.
Count Courses per Student
SELECT s.Name,
COUNT(c.CourseID) AS TotalCourses
FROM Students s
JOIN Courses c
ON s.StudentID = c.StudentID
GROUP BY s.Name;â ī¸ GROUP BY Rules
| Rule | Description |
|---|---|
| Grouped Columns | Every non-aggregated column in the SELECT list should appear in the GROUP BY clause. |
| Aggregate Functions | Aggregate functions operate on each group individually. |
| WHERE | Filters rows before grouping. |
| HAVING | Filters groups after aggregation. |
đ Common Aggregate Functions with GROUP BY
| Function | Purpose |
|---|---|
| COUNT() | Counts records. |
| SUM() | Calculates totals. |
| AVG() | Calculates averages. |
| MIN() | Returns the smallest value. |
| MAX() | Returns the largest value. |
đŧ Real-World Example
A university administrator wants to generate a report showing the number of students, average marks, and total fees collected for each department.
Department Summary Report
SELECT Department,
COUNT(*) AS StudentCount,
AVG(Marks) AS AverageMarks,
SUM(FeesPaid) AS TotalFees
FROM Students
GROUP BY Department
ORDER BY StudentCount DESC;This query creates a summary report that combines multiple aggregate functions for every department.
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ GROUP BY groups rows with identical values.
- đ It is commonly used with aggregate functions.
- đ WHERE filters rows before grouping.
- đ HAVING filters groups after aggregation.
- đ Multiple columns can be used in the GROUP BY clause.
- đ ORDER BY can sort grouped results.