Where And Having Clause Difference In SQL Server 2008

In this article I will show you the major differences between WHERE clause and Having clause.
  • 1648

In this article I will show you the major differences between WHERE clause and Having clause.

Most of the times, we get confused between WHERE clause Having clause. Both Having Clause and Where clause filters the data coming from the Select statement, but still they are different to each other. The major scenarioes on the basis of which we can differenciate are explained below:

Scenario 1

Having is used only with the SELECT statement but  WHERE clause can be used with Select, Update and Delete statement.

Where Clause

whereVShavingClause01.jpg

Having Clause

whereVShavingClause02.jpg

Scenario 2

Having Clause is used to filter group while WHERE clause is used to filter rows.

Scenario 3

Most of the time WHERE or Having clause give same result. Below given two SQL statements gives the same result. Suppose I want the Users grouped by DeapartmentID. For doing this, use GROUP BY clause to build an aggregate query.

Where Clause

whereVShavingClause03.jpg

Having Clause

whereVShavingClause04.jpg

Working

Most of the time you will get the same result with Where or Having . WHERE clause is used to fetch particular records and this clause is applied after select statement but Having clause is  used for combination of group by functions. Having clause is typically used in GROUP BY clause, without using GROUP BY function  you can not write the having clause.

Having clause works only to columns that also appear in the Group by clause or in an aggregate function.

Where clause is applied prior the grouping of data, while  Having clause is specified  after grouping the result of the data.

© 2018 DotNetHeaven. All rights reserved.