Step 7
4 min read

HAVING Clause

Learn how to filter grouped data using the HAVING clause.

What is HAVING?

Sample Students Table
idnameagegrade
1John20A
2Mary19A
3Peter21A
4Sarah20B
5Tom22B
6Lisa18C
6 rows
WHERE vs HAVING Comparison
AspectWHEREHAVING
PurposeFilter rowsFilter groups
WhenBefore GROUP BYAfter GROUP BY
Works onIndividual rowsGrouped results
Can use aggregatesNoYes
ExampleWHERE age > 18HAVING COUNT(*) > 5
5 rows

HAVING filters groups created by GROUP BY. It's like GROUP BY creates categories, and HAVING picks which categories to show.

Simple example: You group students by grade (A, B, C). HAVING shows only grades with 3 or more students.

WHERE vs HAVING

Sample Students Table
idnameagegrade
1John20A
2Mary19A
3Peter21A
4Sarah20B
5Tom22B
6Lisa18C
6 rows
WHERE vs HAVING Comparison
AspectWHEREHAVING
PurposeFilter rowsFilter groups
WhenBefore GROUP BYAfter GROUP BY
Works onIndividual rowsGrouped results
Can use aggregatesNoYes
ExampleWHERE age > 18HAVING COUNT(*) > 5
5 rows

WHERE - Filters individual rows BEFORE grouping HAVING - Filters groups AFTER grouping

Think of it like:

  • WHERE: "Pick only ripe apples" → Then group by color
  • HAVING: Group all apples by color → "Show only colors with 10+ apples"

Basic Syntax

Sample Students Table
idnameagegrade
1John20A
2Mary19A
3Peter21A
4Sarah20B
5Tom22B
6Lisa18C
6 rows
WHERE vs HAVING Comparison
AspectWHEREHAVING
PurposeFilter rowsFilter groups
WhenBefore GROUP BYAfter GROUP BY
Works onIndividual rowsGrouped results
Can use aggregatesNoYes
ExampleWHERE age > 18HAVING COUNT(*) > 5
5 rows
SELECT column, COUNT(*) FROM table GROUP BY column HAVING COUNT(*) > 5;

Simple Examples

Sample Students Table
idnameagegrade
1John20A
2Mary19A
3Peter21A
4Sarah20B
5Tom22B
6Lisa18C
6 rows
WHERE vs HAVING Comparison
AspectWHEREHAVING
PurposeFilter rowsFilter groups
WhenBefore GROUP BYAfter GROUP BY
Works onIndividual rowsGrouped results
Can use aggregatesNoYes
ExampleWHERE age > 18HAVING COUNT(*) > 5
5 rows

Example 1: Grades with Many Students

Result: Grades with 3+ Students (HAVING COUNT(*) >= 3)
gradestudent_count
A3
1 row
SELECT grade, COUNT(*) AS total FROM students GROUP BY grade HAVING COUNT(*) >= 3;

Shows only grades with 3+ students.

Example 2: High Spending Customers

Result: High Salary Departments
departmentavg_salary
Engineering85000
Sales65000
2 rows
SELECT customer_id, SUM(total) AS spent FROM orders GROUP BY customer_id HAVING SUM(total) > 1000;

Shows only customers who spent over $1000.

Example 3: Both WHERE and HAVING

Result: Products with 100+ Sales
product_nametotal_sold
Mouse150
Laptop125
2 rows
SELECT grade, COUNT(*) AS total FROM students WHERE age >= 18 GROUP BY grade HAVING COUNT(*) >= 2;
  1. WHERE picks students aged 18+
  2. GROUP BY groups them by grade
  3. HAVING shows only grades with 2+ students

Quick Rule

Sample Students Table
idnameagegrade
1John20A
2Mary19A
3Peter21A
4Sarah20B
5Tom22B
6Lisa18C
6 rows
WHERE vs HAVING Comparison
AspectWHEREHAVING
PurposeFilter rowsFilter groups
WhenBefore GROUP BYAfter GROUP BY
Works onIndividual rowsGrouped results
Can use aggregatesNoYes
ExampleWHERE age > 18HAVING COUNT(*) > 5
5 rows
  • Use WHERE to filter rows (before grouping)
  • Use HAVING to filter groups (after grouping)
  • HAVING needs GROUP BY, WHERE doesn't

Summary

Sample Students Table
idnameagegrade
1John20A
2Mary19A
3Peter21A
4Sarah20B
5Tom22B
6Lisa18C
6 rows
WHERE vs HAVING Comparison
AspectWHEREHAVING
PurposeFilter rowsFilter groups
WhenBefore GROUP BYAfter GROUP BY
Works onIndividual rowsGrouped results
Can use aggregatesNoYes
ExampleWHERE age > 18HAVING COUNT(*) > 5
5 rows

Key points to remember:

  1. HAVING filters groups created by GROUP BY
  2. WHERE filters rows, HAVING filters groups
  3. HAVING must be used with GROUP BY
  4. Use HAVING for aggregate conditions
  5. Can combine multiple conditions with AND/OR
  6. Can use both WHERE and HAVING together
  7. WHERE executes before grouping
  8. HAVING executes after grouping
  9. HAVING can reference aggregate functions
  10. WHERE cannot reference aggregate functions

What Comes Next

Sample Students Table
idnameagegrade
1John20A
2Mary19A
3Peter21A
4Sarah20B
5Tom22B
6Lisa18C
6 rows
WHERE vs HAVING Comparison
AspectWHEREHAVING
PurposeFilter rowsFilter groups
WhenBefore GROUP BYAfter GROUP BY
Works onIndividual rowsGrouped results
Can use aggregatesNoYes
ExampleWHERE age > 18HAVING COUNT(*) > 5
5 rows

Congratulations! You have completed grouping and filtering groups. Next, you will learn about Data Constraints to ensure data integrity.

SQL Editor
Loading...

Show grades with 2 or more students

Output

Click "Run Query" to see results

SQL Editor
Loading...

Grades with average age over 20

Output

Click "Run Query" to see results

SQL Editor
Loading...

WHERE and HAVING together

Output

Click "Run Query" to see results

Finished this topic?

Mark it complete to track your progress and maintain your streak!

SkillsetMaster - AI, Web Development & Data Analytics Courses