HAVING Clause in SQL

11/22/2025
All Articles

SQL HAVING clause tutorial with examples for beginners

HAVING Clause in SQL

HAVING Clause in SQL – SQL Tutorial for Beginners

The HAVING clause in SQL is used to filter records after they have been grouped using the GROUP BY clause. It is similar to the WHERE clause, but WHERE cannot be used with aggregate functions—HAVING can.

In this SQL tutorial, you will learn:

  • What the HAVING clause is

  • Difference between WHERE and HAVING

  • Syntax of HAVING

  • Examples with aggregate functions

  • GROUP BY + HAVING combinations

  • Real-world use cases

  • Best practices


πŸ”Ή What Is the HAVING Clause?

The HAVING clause filters grouped records. It is used when you want to apply a condition on a summary result like:

  • Total salary

  • Count of employees

  • Average marks

  • Number of orders per customer

Since WHERE cannot be used with aggregate functions like SUM(), COUNT(), or AVG(), the HAVING clause is necessary.


πŸ”Έ Syntax of HAVING Clause

SELECT column1, aggregate_function(column2)
FROM table_name
GROUP BY column1
HAVING condition;

πŸ”Ή Example Table: employees

id name department salary
1 Amit IT 50000
2 Neha HR 45000
3 Rahul IT 60000
4 Sara HR 55000
5 Karan Finance 70000

πŸ”Έ Example 1: Departments with total salary above 1,00,000

SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department
HAVING SUM(salary) > 100000;

βœ” Output

department total_salary
IT 110000
HR 100000+

πŸ”Έ Example 2: Departments with more than 1 employee

SELECT department, COUNT(*) AS total_employees
FROM employees
GROUP BY department
HAVING COUNT(*) > 1;

πŸ”Έ Example 3: HAVING with multiple conditions

SELECT department, AVG(salary) AS average_salary
FROM employees
GROUP BY department
HAVING AVG(salary) > 50000
AND COUNT(*) >= 2;

πŸ”Ή WHERE vs HAVING

Feature WHERE HAVING
Filters rows before grouping βœ” ❌
Filters rows after grouping ❌ βœ”
Works with aggregate functions ❌ βœ”
Used with SELECT and UPDATE βœ” ❌

Example: WHERE + GROUP BY + HAVING

SELECT department, SUM(salary) AS total_salary
FROM employees
WHERE salary > 40000
GROUP BY department
HAVING SUM(salary) > 100000;

πŸ”Ή Real-World Examples

βœ” Example: Customers who placed more than 5 orders

SELECT customer_id, COUNT(*) AS total_orders
FROM orders
GROUP BY customer_id
HAVING COUNT(*) > 5;

βœ” Example: Products sold more than 100 units

SELECT product_id, SUM(quantity) AS total_sold
FROM sales
GROUP BY product_id
HAVING SUM(quantity) > 100;

βœ” Example: Cities with average order value above 1,000

SELECT city, AVG(order_amount) AS avg_amount
FROM orders
GROUP BY city
HAVING AVG(order_amount) > 1000;

πŸ”Ή Best Practices

βœ” Use WHERE for row-level filtering before grouping
βœ” Use HAVING for aggregated filtering after grouping
βœ” Keep HAVING conditions simple for better performance
βœ” Use proper indexes on GROUP BY columns
βœ” Always pair GROUP BY with meaningful aggregate functions


Summary

In this SQL HAVING clause tutorial, you learned:

  • What HAVING does

  • How it filters grouped records

  • Difference between WHERE and HAVING

  • Examples using COUNT, SUM, AVG

  • Real-world scenarios

HAVING is essential for analytical queries and generating summary-based reports.

Article