Beginner

389. HAVING Clause

Learn how to filter grouped results after aggregate calculations.

📘 Lesson 389 of 728 ⏱ 11 min 🎯 Intermediate

HAVING filters groups produced by GROUP BY, usually based on aggregate results.

HAVING is essential when a business requirement asks for groups meeting a calculated threshold.

Find departments with more than 10 employees.

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

HAVING is used for sales thresholds, customer counts, department analysis, transaction summaries, and KPI reporting.

  • HAVING filters groups.
  • WHERE filters rows before grouping.
  • HAVING is commonly used with aggregate functions.
  • It is applied after grouping logically.
  • It can contain multiple conditions.
What is the difference between WHERE and HAVING?

WHERE filters individual rows before grouping, while HAVING filters grouped results after aggregation.

Can HAVING be used without GROUP BY?

Yes, in many database systems HAVING can filter the result of aggregate queries even without an explicit GROUP BY.

Have you finished this lesson?

Introduction to SQL – All Lessons