![SQL having and where clause](https://techniti.org/wp-content/uploads/2023/09/WHERE-VS-HAVING.png)
SQL having clause is used where you have to use some kind of aggregate as a condition.
SYNTAX:
SELECT *
FROM DEMO_TAB
GROUP BY COL
HAVING CONDITION
ORDER BY COL
EXAMPLE
This is a demo table.
![](https://techniti.org/wp-content/uploads/2023/09/1.png)
Now suppose you have to find the subjects with average marks more than 80, here is how you will do it.
SELECT SUBJECT, AVG(MARKS) AS AVERAGE
FROM MARKS_DATA
GROUP BY SUBJECT
HAVING AVG(MARKS) >80
![](https://techniti.org/wp-content/uploads/2023/09/2.png)
Here is what happens when you use WHERE clause instead of the HAVING clause.
SELECT SUBJECT, AVG(MARKS) AS AVERAGE
FROM MARKS_DATA
GROUP BY SUBJECT
WHERE AVG(MARKS) >80
![](https://techniti.org/wp-content/uploads/2023/09/3.png)
Error occurred because we cannot use aggregates as condition in SQL query.
READ MORE: Learn SQL Fast
Source: Microsoft Documentation