The main difference between
WHERE and HAVING clause comes when used together with GROUP BY clause, in that
case WHERE is used to filter rows before grouping and HAVING is used to exclude
records after grouping. This is the most important difference and if you
remember this, it will help you write better SQL queries. This is also one of
the important SQL concepts to understand, not just from an interview
perspective but also from a day-to-day use perspective. I am sure you have used
WHERE clause because it’s one of the most common clauses in SQL along with
SELECT and used to specify the filtering criterion or condition. You can even use
WHERE clause without HAVING or GROUP BY, as you have seen it many times.
On the other hand, HAVING can only be used if the grouping has been performed using the GROUP BY clause in the SQL query. Another
worth noting thing about WHERE and HAVING clause is that WHERE clause cannot
contain aggregate function like COUNT(), SUM(), MAX(), MIN(), etc. but The HAVING clause may contain aggregate functions.
Another worth noting the difference between
WHERE and HAVING clause is that WHERE is used to impose filtering criterion on
a SELECT, UPDATE, DELETE statement as well as single row function and use
before group by clause but HAVING is always used after group by clause.