HAVING Clause

The HAVING clause was added to SQL because the WHERE keyword cannot be used after aggregate functions to group the result-set.

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

Difference between ORDER BY, GROUP BY, and HAVING CLAUSE

Clause
Purpose
Aggregate Functions Allowed?
Execution Order
Example Use Case

WHERE

Filters individual rows before aggregation

❌ No

Before GROUP BY

Find employees with salary > 50,000

GROUP BY

Groups rows and applies aggregate functions

✅ Yes

After WHERE, before HAVING

Find the highest salary per department

HAVING

Filters aggregated results after GROUP BY

✅ Yes

After GROUP BY

Find departments where max salary > 80,000

Last updated