HAVING Clause in SQL

11/22/2025

SQL HAVING clause tutorial with examples for beginners

Go Back

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


SQL HAVING clause tutorial with examples for beginners

🔹 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

idnamedepartmentsalary
1AmitIT50000
2NehaHR45000
3RahulIT60000
4SaraHR55000
5KaranFinance70000

🔸 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

departmenttotal_salary
IT110000
HR100000+

🔸 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

FeatureWHEREHAVING
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.