Re: <> ALL( ... ) and <> ANY ( ... ) didn't behave as expected

From: "John D(dot) Burger" <john(at)mitre(dot)org>
To: General PostgreSQL List <pgsql-general(at)postgresql(dot)org>
Subject: Re: <> ALL( ... ) and <> ANY ( ... ) didn't behave as expected
Date: 2007-10-10 18:12:07
Message-ID: 258F261A-1377-4FF8-A565-557E9CEE5632@mitre.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Richard Broersma Jr wrote:

> Here is the example that doesn't do what I expect:
>
> --find all parents that have a mixture of boys and girls.
> --but this doesn't return anything
> SELECT *
> FROM Parents AS P
> WHERE 'girl' <> ALL ( SELECT gender
> FROM Children AS C1
> WHERE C1.parentid = P.parentid )
> AND 'boy' <> ALL ( SELECT gender
> FROM Children AS C1
> WHERE C1.parentid = P.parentid );

I read this as: Find all parents such that none of their kids are
girls and none of their kids are boys. That is, ALL of their genders
are <> 'girl', AND ALL of their genders are <> 'boy'. Under the
obvious assumptions about gender, the result is of course empty -
except it's not clear to me what should happen for childless people ...

> --This query gets what I want
> --but I believe that it shouldn't return anything
>
> SELECT *
> FROM Parents AS P
> WHERE 'girl' <> ANY ( SELECT gender
> FROM Children AS C1
> WHERE C1.parentid = P.parentid )
> AND 'boy' <> ANY ( SELECT gender
> FROM Children AS C1
> WHERE C1.parentid = P.parentid );

I read this as: Find all parents such that at least one of their
kids is not a girl, and at least one of their kids is not a boy. Of
course, this could also be written with =.

- John Burger
MITRE

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2007-10-10 18:17:01 Re: <> ALL( ... ) and <> ANY ( ... ) didn't behave as expected
Previous Message Richard Broersma Jr 2007-10-10 17:38:39 <> ALL( ... ) and <> ANY ( ... ) didn't behave as expected