The Difference Between HAVING and WHERE Clause

During your IT interview, you may be asked to state the difference between HAVING and WHERE clause in SQL. It is actually a very common question for entry- and mid-level interviews. We are going to tell you the main things you need to know to answer this question correctly and impress the interviewers.

The Main Difference Between HAVING and WHERE Clause in SQL

Assuming that the HAVING and WHERE clauses are interchangeable is actually a common mistake made by many interview candidates. In spite of the fact that the two clauses are similar, there are some very important differences between them:

  • The WHERE clause can be used with the GROUP BY clause, whereas the HAVING clause cannot be used with the GROUP BY clause.
  • The WHERE clause specifies the specific criteria that individual records must be in line with in order to be selected by a query.
  • While the HAVING clause selects rows after grouping, the WHERE clause selects rows before grouping.
  • While the HAVING function can include aggregate functions, the WHERE clause cannot include aggregate functions.

What you need to remember for your interview is that the main dissimilarity between the HAVING and WHERE clause in SQL is that the WHERE clause can be used to filter results. The records that do not meet the criteria set up in the WHERE clause will not pass the condition and they will not be included in the results. The HAVING clause can then be used to filter the summarized or grouped data.

Image wikimedia

Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.