đ¯ The HAVING clause in SQL is used to filter grouped data after aggregate functions have been applied. It is commonly used with the GROUP BY clause to return only those groups that satisfy a specified condition. Unlike WHERE, which filters individual rows, HAVING filters groups.
đ What is the HAVING Clause?
The HAVING clause allows you to specify conditions on aggregate values such as counts, sums, averages, minimums, and maximums. It is evaluated after rows have been grouped.
Information
đ Basic Syntax
HAVING Syntax
SELECT column_name,
aggregate_function(column_name)
FROM table_name
WHERE condition
GROUP BY column_name
HAVING aggregate_function(column_name) condition;đ 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ī¸âŖ HAVING with COUNT()
Display departments that have more than one student.
HAVING with COUNT()
SELECT Department,
COUNT(*) AS TotalStudents
FROM Students
GROUP BY Department
HAVING COUNT(*) > 1;Result:
| Department | TotalStudents |
|---|---|
| Computer Science | 2 |
| Mathematics | 2 |
2ī¸âŖ HAVING with SUM()
Find departments whose total fees collected exceed 90000.
HAVING with SUM()
SELECT Department,
SUM(FeesPaid) AS TotalFees
FROM Students
GROUP BY Department
HAVING SUM(FeesPaid) > 90000;3ī¸âŖ HAVING with AVG()
Display departments whose average marks are greater than 85.
HAVING with AVG()
SELECT Department,
AVG(Marks) AS AverageMarks
FROM Students
GROUP BY Department
HAVING AVG(Marks) > 85;4ī¸âŖ HAVING with MIN() and MAX()
Display departments whose highest marks exceed 90.
HAVING with MAX()
SELECT Department,
MAX(Marks) AS HighestMarks
FROM Students
GROUP BY Department
HAVING MAX(Marks) > 90;đ HAVING with Multiple Conditions
You can combine multiple conditions using AND and OR.
Multiple HAVING Conditions
SELECT Department,
COUNT(*) AS TotalStudents,
AVG(Marks) AS AverageMarks
FROM Students
GROUP BY Department
HAVING COUNT(*) > 1
AND AVG(Marks) > 85;đ HAVING with WHERE
The WHERE clause filters rows before grouping, while HAVING filters groups after aggregation.
WHERE and HAVING Together
SELECT Department,
AVG(Marks) AS AverageMarks
FROM Students
WHERE Marks >= 80
GROUP BY Department
HAVING AVG(Marks) > 85;Tip
1ī¸âŖ WHERE filters rows.
2ī¸âŖ GROUP BY creates groups.
3ī¸âŖ Aggregate functions calculate results.
4ī¸âŖ HAVING filters the groups.
đ HAVING with ORDER BY
Use ORDER BY to sort grouped results after the HAVING clause.
HAVING with ORDER BY
SELECT Department,
AVG(Marks) AS AverageMarks
FROM Students
GROUP BY Department
HAVING AVG(Marks) > 80
ORDER BY AverageMarks DESC;đ HAVING with JOIN
The HAVING clause is frequently used with joins to filter aggregated data across related tables.
HAVING with JOIN
SELECT s.Name,
COUNT(c.CourseID) AS TotalCourses
FROM Students s
JOIN Courses c
ON s.StudentID = c.StudentID
GROUP BY s.Name
HAVING COUNT(c.CourseID) >= 3;âī¸ WHERE vs HAVING
| Feature | WHERE | HAVING |
|---|---|---|
| Filters | Individual rows. | |
| Groups. | ||
| Uses Aggregate Functions | No. | Yes. |
| Execution Stage | Before grouping. | After grouping. |
| Common Usage | Filter raw data. | Filter summary data. |
đ Common Aggregate Functions with HAVING
| 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 a report showing only departments that have at least two students and an average mark above 85.
Department Performance Report
SELECT Department,
COUNT(*) AS StudentCount,
AVG(Marks) AS AverageMarks,
SUM(FeesPaid) AS TotalFees
FROM Students
GROUP BY Department
HAVING COUNT(*) >= 2
AND AVG(Marks) > 85
ORDER BY AverageMarks DESC;This query generates a summarized report while excluding departments that do not meet the specified criteria.
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ HAVING filters grouped data.
- đ It is commonly used with GROUP BY.
- đ It supports aggregate functions such as COUNT(), SUM(), AVG(), MIN(), and MAX().
- đ WHERE filters rows before grouping, while HAVING filters groups after grouping.
- đ Multiple conditions can be combined using AND and OR.
- đ ORDER BY can be used to sort grouped results.