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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: