Difference between WHERE and HAVING clause in SQL Server

What is difference between WHERE and HAVING clause in SQL is one of the most popular question asked on SQL and database interviews, especially for beginners.
 
WHERE
  • We can use WHERE clause with SELECT, INSERT, UPDATE and DELETE clause. For Example it works fine for "Update Mas_Employee Set Salary = 1500 WHERE Id =1".

  • WHERE clause is used for filtering rows and it applies on each and every row.

  • WHERE clause is used before GROUP BY clause.

  • We can't use aggregate functions in the where clause unless it is in a sub query contained in a HAVING clause.
HAVING
  • HAVING clause can only be used with SELECT query. Means if you want perform INSERT, UPDATE and DELETE clause it will retuns an error. For Example "Update Mas_Employee Set Salary = 1500 Having Id =1" Query will be generated error like "Incorrect syntax near the keyword 'HAVING'. ".

  • HAVING clause is used to filter groups in SQL.

  • HAVING clause is used after GROUP BY clause.

  • We can use aggregate function in HAVING clause.
Please provide your valuable suggestions and feedback if you found this article helpful.