Re: difference between 'where' and 'having'

From: "Richard Broersma" <richard(dot)broersma(at)gmail(dot)com>
To: Adam Šindelář <adam(dot)sindelar(at)gmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: difference between 'where' and 'having'
Date: 2008-04-28 19:45:12
Message-ID: 396486430804281245h375d6d2cu4add4b85c1e59ac4@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

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.

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2008-04-28 19:56:57 Re: difference between 'where' and 'having'
Previous Message Adam Šindelář 2008-04-28 19:22:11 difference between 'where' and 'having'