"if you encounter any errors in SAP, send me a screenshot at pramod@learntosap.com, and I will help you resolve the issue."

SQL HAVING Clause

SQL TUTORIALS-

SQL HAVING Clause

Introduction-


Trulli Trulli

βœ…In a Simple Word The HAVING clause in SQL is used to filter groups of rows created by the GROUP BY clause β€” similar to how WHERE filters individual rows...


πŸ’‘ Note: The SQL HAVING clause filters aggregated results after the GROUP BY operation is performed.

βœ… Syntax Example -



SELECT column_name, AGGREGATE_FUNCTION(column_name)
FROM table_name
GROUP BY column_name
HAVING condition;

πŸ’‘ Note: The SQL WHERE clause Filters individual rows (before grouping).
Emoji Examples in HTML

πŸ”Έ Example -)Table: Employee


EmpID Name Department Salary
1 ANNI HR 40000
2 RAJ HR 45000
3 POOJA IT 55000
4 SUJAN IT 60000
5 RUPESH SALES 30000
6 SIMA SALES 32000

βœ… Example 1:-Show departments where total salary > 80000-



SELECT Department, SUM(Salary) AS TotalSalary
FROM Employee
GROUP BY Department
HAVING SUM(Salary) > 80000;

βœ…Result Show :-

Result β†’

Emoji Examples in HTML


Department TotalSalary
HR 85000
IT 115000

βœ… Example 2: Departments with more than 1 (ONE) Employee-



SELECT Department, COUNT(*) AS EmployeeCount
FROM Employee
GROUP BY Department
HAVING COUNT(*) > 1;

βœ…Result Show :-

Result β†’

Emoji Examples in HTML


Department EmployeeCount
HR 2
IT 2
SALES 2

βœ… Example 3: Departments with average salary below 40000-



SELECT Department, AVG(Salary) AS AvgSalary
FROM Employee
GROUP BY Department
HAVING AVG(Salary) < 40000;

βœ…Result Show :-

Result β†’

Emoji Examples in HTML


Department AvgSalary
SALES 31000

βœ… Example 4:Only show departments with total salary between 60000 and 90000-



SELECT Department, SUM(Salary) AS TotalSalary
FROM Employee
GROUP BY Department
HAVING SUM(Salary) BETWEEN 60000 AND 90000;

βœ…Result Show :-

Result β†’

Emoji Examples in HTML


Department TotalSalary
HR 85000
SALES 62000

Practice - Yes/No Quiz

1.Is the HAVING clause used after GROUP BY?

2.Can you use the HAVING clause without GROUP BY?

3.Can HAVING filter rows before grouping happens?


May Be Like Important Link-

-Credit Memo and Return PO

-Bill Of Material (BOM)

-How To Create Quotation

-How To Create Debit Memo Request