Beginner

75. HAVING Clause

Learn how to filter grouped results using the HAVING clause.

📘 Lesson 75 of 728 ⏱ 8 min 🎯 Beginner

The HAVING clause filters groups created by GROUP BY. While WHERE normally filters individual rows before grouping, HAVING filters the aggregated groups after grouping.

Analysts often need to return only groups that meet a business condition, such as departments having more than 10 employees or regions generating more than ₹1 crore in sales.

To find departments with more than five employees, GROUP BY creates department groups and HAVING filters those groups based on COUNT(*).

SQL
SELECT
    Department,
    COUNT(*) AS EmployeeCount
FROM Employees
GROUP BY Department
HAVING COUNT(*) > 5;

HAVING is commonly used in sales reporting, customer analysis, employee reporting, financial summaries, and business performance analysis.

  • HAVING filters grouped results.
  • HAVING is commonly used with GROUP BY.
  • HAVING can use aggregate functions.
  • WHERE filters rows before grouping.
  • HAVING filters groups after aggregation.
What does HAVING do in SQL?

HAVING filters groups produced by GROUP BY.

What is the difference between WHERE and HAVING?

WHERE normally filters individual rows before grouping, while HAVING filters groups after aggregation.

Have you finished this lesson?

Introduction to SQL – All Lessons