Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search archives
  Advanced Search

Re: difference between 'where' and 'having'



On Mon, Apr 28, 2008 at 12:22 PM, Adam Šindelář <adam(dot)sindelar(at)gmail(dot)com> wrote:

> Expressions in the HAVING clause can refer both to grouped expressions and
> to ungrouped expressions (which necessarily involve an aggregate function).

True, however, when constraing data in a having clause, alot more data
will be processed before it is grouped.  This mean that putting all of
the costraints in the having clause will result in poorly performing
queries.

> I've been happily working with Postgres for a long time, and not once have I
> ever used a HAVING in my queries, even in fairly complex ones, and I just
> can't bear the suspense anymore! :)

I only use the HAVING clause in cases where the where clause cannot
easy constrain the data without a sub-query.

SELECT AVG( X ), y
  FROM Thetable
 WHERE y > 10
GROUP BY Y
 HAVING AVG( X ) > 3 AND COUNT( * ) > 10 AND MAX( X ) < 10;


-- 
Regards,
Richard Broersma Jr.


Home | Main Index | Thread Index

Privacy Policy | About PostgreSQL
Copyright © 1996 – 2012 PostgreSQL Global Development Group