Where Vs Having In SQL

21-05-2012

The Having clause can only be used when you are using the group by phrase.

When you are using the Group By phrase in a query, you can use the Having clause for pretty much everything you can do with the Where clause. But not vise versa.

The where clause doesn’t support aggregate functions so you must include any aggregate functions in the having clause.

In the below example, you can place the ” Department = ‘Science’ ” in either the Where Clause or the Having Clause but you must include the ” Max(Mark) > 50 ” in the Having Clause.

Ex. This will return the highest mark scored for each subject in the Science department
SELECT Max(Mark) From Results
Group By SubjectId
Having Max(Mark) > 50
Where Department = ‘Science’

Menol

This entry was posted in Database-Concepts, Oracle, SQL Server and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *