Re: having clause question - Mailing list pgsql-general

From Tom Lane
Subject Re: having clause question
Date
Msg-id 22896.1099177941@sss.pgh.pa.us
Whole thread Raw
In response to Re: having clause question  (Ian Barwick <barwick@gmail.com>)
List pgsql-general
Ian Barwick <barwick@gmail.com> writes:
> On Sat, 30 Oct 2004 15:17:16 -0700, Shane Wegner
> <shane-keyword-pgsql.a1e0d9@cm.nu> wrote:
>> Under MySQL, this query does the trick.
>> select lastname,count(*) as c from names group by lastname
>> having c > 1;
>>
>> Is it possible to do a query like this with PostgreSQL?

> select lastname,count(*) as c from names group by lastname having count(*) > 1;

Just to expand on that: MySQL's abbrevation is not legal SQL, and it's
not even very sensible, because logically speaking the SELECT output
list is only evaluated after (and if) the HAVING clause succeeds.
So it makes no sense for the HAVING clause to refer to SELECT values.

Postgres does optimize the case of multiple identical aggregate-function
invocations in a query, BTW, so the apparent inefficiency is not real.

            regards, tom lane

pgsql-general by date:

Previous
From: Ian Barwick
Date:
Subject: Re: having clause question
Next
From: Ken Tozier
Date:
Subject: Superuser log-in through a web interface?