X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id C1CBFD6D5A for ; Sun, 13 Nov 2005 05:16:19 -0400 (AST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 26296-01 for ; Sun, 13 Nov 2005 09:16:15 +0000 (GMT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by svr1.postgresql.org (Postfix) with ESMTP id F0922D6811 for ; Sun, 13 Nov 2005 05:16:16 -0400 (AST) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 9753C3555F; Sun, 13 Nov 2005 01:16:18 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 95A793550E; Sun, 13 Nov 2005 01:16:18 -0800 (PST) Date: Sun, 13 Nov 2005 01:16:18 -0800 (PST) From: Stephan Szabo To: Steve SAUTETNER Cc: pgsql-sql@postgresql.org Subject: Re: Problem with "NOT IN (subquery) In-Reply-To: Message-ID: <20051113010417.D61269@megazone.bigpanda.com> References: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN Content-Transfer-Encoding: QUOTED-PRINTABLE X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.006 required=5 tests=[AWL=0.006] X-Spam-Score: 0.006 X-Spam-Level: X-Archive-Number: 200511/111 X-Sequence-Number: 23362 On Sun, 13 Nov 2005, Steve SAUTETNER wrote: > Hi, > > I have a table named "famille" whose structure and content is : > > famille_code | famille_mere_famille_code | famille_libelle | > famille_niveau > --------------+---------------------------+-------------------+----------= --- > --- > 000000 | | Mhre | Is that a NULL famille_mere_famille_code? > The first col is the family id and the second is the mother family id. > I would get a list of all families that are never in the col n=B02, so > the families that aren't node but leaf. > > The query, i made was "SELECT * FROM famille WHERE famille_code NOT IN > (SELECT DISTINCT famille_mere_famille_code FROM famille);" > > But the DB returns 0 records instead of 15. If i use a list instead of a > subquery it works normaly but it's not easy to manage it like this. > > So if anyone can help me please ... In the case where the subselect returns a NULL, the behavior of IN and NOT IN is rather unfortunate. A NOT IN B is basically NOT(A IN B) and A IN B is basically A =3DANY B IIRC. However, A=3DANY B only returns false if A = =3D Bi returns false for all Bi contained in B and A =3D NULL returns unknown, not false, so NOT IN cannot return true if the subselect contains a NULL. If that is a null above, probably the best solution is to exclude NULLs from the subselect results.