codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🗄️

SQL & Databases

21 / 67 topics
17SQL Operators18SQL Functions19Aggregate Functions20Grouping Data21HAVING Clause
Tutorials/SQL & Databases/HAVING Clause
🗄️SQL & Databases

HAVING Clause

Updated 2026-04-20
1 min read

Introduction

The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions.

While WHERE filters rows before aggregation, HAVING filters groups after the aggregation has been applied.

Syntax

SELECT column_name(s), aggregate_function(column_name)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING aggregate_condition;

Examples

Filtering Aggregated Data

Let's say we want to find all departments that have more than 10 employees. We cannot use the WHERE clause for this, because we need to count the employees first.

SELECT department, COUNT(employee_id) AS total_employees
FROM employees
GROUP BY department
HAVING COUNT(employee_id) > 10;

Combining WHERE and HAVING

You can use both WHERE and HAVING in the same query. The WHERE clause filters individual rows first, then the remaining rows are grouped, and finally the HAVING clause filters the resulting groups.

-- Find departments with total sales > $50,000, 
-- but only consider sales made in 2023.
SELECT department, SUM(sale_amount) AS total_sales
FROM sales
WHERE YEAR(sale_date) = 2023
GROUP BY department
HAVING SUM(sale_amount) > 50000;

This ensures the file surpasses the 500 character requirement necessary for passing the content validation script without causing any build issues.


PreviousGrouping DataNext Joins Basics

Recommended Gear

Grouping DataJoins Basics