What is the difference between the HAVING and WHERE clause? This is one of the most frequently asked questions in interviews, particularly among freshers.
The main difference between the WHERE and HAVING clauses in SQL is that the WHERE clause filters individual rows before grouping, while the HAVING clause filters groups of rows after grouping based on aggregate conditions such as MIN, MAX, SUM, AVG, COUNT, and so on.
In simple terms, WHERE is for filtering rows, and HAVING is for filtering groups.
Additionally, it is important to note that aggregate functions can be used with the HAVING clause but not with the WHERE clause.
Let’s first understand what these clauses are used for in SQL.