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

From Richard Broersma
Subject Re: difference between 'where' and 'having'
Date
Msg-id 396486430804281245h375d6d2cu4add4b85c1e59ac4@mail.gmail.com
Whole thread Raw
In response to difference between 'where' and 'having'  ("Adam Šindelář" <adam.sindelar@gmail.com>)
List pgsql-novice
On Mon, Apr 28, 2008 at 12:22 PM, Adam Šindelář <adam.sindelar@gmail.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.

pgsql-novice by date:

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