Difference between Having and Where Clause

Key difference: Both, ‘Having’ and ‘Where’ are clauses that can be utilized in SQL. The ‘Where’ clause is applied first to the individual rows in the tables, using which the rows that meet the conditions in the clause are grouped together. The ‘Having’ clause is then applied to the rows in the result set. The groups that meet the conditions in the ‘Having’ clause appear in the query output.

Both ‘Having’ and ‘Where’ are clauses that can be utilized in SQL. SQL is a special-purpose programming language designed for managing data in relational database management systems (RDBMS). It has the following functions:

  • Data insert,
  • Update and delete,
  • Access and control, and
  • Schema control and modification.

It is subdivided into elements, such as:

  • Expressions
  • Predicates
  • Queries
  • Statements
  • Clauses

The clauses in SQL include:

  • Constraint clause
  • For Update clause
  • From clause
  • Group By clause
  • Order By clause
  • Where Current Of clause
  • Having clause
  • Where clause

‘Having’ and ‘Where’ are just two of these many clauses. Statements in SQL can be executed in the following order:

  • FROM clause
  • WHERE clause
  • GROUP BY clause
  • HAVING clause
  • SELECT clause
  • ORDER BY clause

The ‘Having’ clause is used only with the ‘Select’ statement and after the ‘Group By’ clause. The ‘Where’ clause, on the other hand, can be used with statement other than the ‘Select’ statement as well. However, it is used before the ‘Group By’ clause. If in case, the ‘Group By’ clause is not used, then the ‘Having’ clause behaves like a ‘Where’ clause.

A query can contain both the ‘Having’ clause and the ‘Where’ clause. The main difference between ‘Having’ and ‘Where’ is that the ‘Having’ clause operates on groups as a whole, whereas the ‘Where’ clause operates on individual rows.

The ‘Where’ clause is applied first to the individual rows in the tables, using which the rows that meet the conditions in the clause are grouped together. The ‘Having’ clause is then applied to the rows in the result set. The groups that meet the conditions in the ‘Having’ clause appear in the query output.

The ‘Having’ clause is used to filter records that are returned by a group by clause utilizing a predicate. By using the ‘Having’ clause, the results of a ‘Group By’ clause can be restricted and only the specified rows are selected.

The ‘Where’ clause, on the other hand, is used to select rows that are to be included in the query. The clause uses a comparison predicate based on the following operators: = or equal, < > or not equal, > or greater than, < or less than, >= or greater than or equal, <= or less than or equal, between, like, and in. The ‘Where’ clause excludes rows that do not meet the requirements as set by the query.

The ‘Where’ clause is used select individual rows in a table which is specified in a ‘From’ clause. It is used to retrieve specific information from a table excluding other irrelevant data. Doing this tends to increase the processing time for the query.

Image Courtesy: c-sharpcorner.com, usefulfreetips.com

Most Searched in Computers and Internets Most Searched in Pregnancy and Parenting
Top 10 Most Searched Differences Most Searched in Sports
Samsung Galaxy Mega 6.3 vs Samsung Galaxy Tab 2 7.0
Spoof vs Parody vs Satire
Asus FonePad vs HP Slate 7
Samsung Galaxy Note 8.0 vs Samsung Galaxy Note 10.1

Add new comment

Plain text

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.