Re: difference between 'where' and 'having' - Mailing list pgsql-novice

From Emil Obermayr
Subject Re: difference between 'where' and 'having'
Date
Msg-id 200804282241.09116.nobs@nobswolf.info
Whole thread Raw
In response to difference between 'where' and 'having'  ("Adam Šindelář" <adam.sindelar@gmail.com>)
List pgsql-novice
Am Montag, 28. April 2008 schrieb Adam Šindelář:
> ... what difference there is between a WHERE clause and a
> HAVING clause besides the syntax?

That's a classic :)

"where" is a selection directly on the data in the database

"having" is a selection on the result

in simple selects, the database-data and the result-data basically is the same

but there are quite simple things you can't do with a "where" like selecting
on the number of records aggregated by a "group by":

select city, count(*) from customers group by city having count(*) > 10;

to see in which cities you have more than 10 customers.

Some databases allow all selections allowed in where also in having. Mostly
this is stupid because it costs performance. having-selections are not
optimized during the database-scan. You should use them only on aggregated
results.

Hope this helps a bit

pgsql-novice by date:

Previous
From: Tom Lane
Date:
Subject: Re: difference between 'where' and 'having'
Next
From: "Adam Šindelář"
Date:
Subject: Re: difference between 'where' and 'having'