Difference between where and having clause. When do we use where function and when do we use having a function

Sql, Function

WHERE clause is used for filtering rows and it applies on each and every row, while HAVING clause is used to filter groups in SQL. One syntax level difference between WHERE and HAVING clause is that the former is used before the GROUP BY clause, while later is used after the GROUP BY clause.

Difference between WHERE and HAVING clause:

  1. WHERE clause can be used with Select, Insert and Update statements, where as HAVING clause can only be used with the Select statement.

  2. Aggregate functions cannot be used in the WHERE clause, unless it is in a sub query contained in a HAVING clause, whereas, aggregate functions can be used in Having clause.

  3. WHERE filters rows before aggregation (GROUPING), where as, HAVING filters groups, after the aggregations are performed.