Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 92B97B5DBEE for ; Sun, 15 May 2011 05:04:55 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 92089-02 for ; Sun, 15 May 2011 08:04:48 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id 6F1BEB5D80B for ; Sun, 15 May 2011 05:04:48 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1QLWJj-0003Wq-52 for pgsql-sql@postgresql.org; Sun, 15 May 2011 10:04:47 +0200 Received: from 212.126.222.122 ([212.126.222.122]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 15 May 2011 10:04:47 +0200 Received: from hari.fuchs by 212.126.222.122 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 15 May 2011 10:04:47 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Harald Fuchs Subject: Re: self join Date: Sun, 15 May 2011 10:04:30 +0200 Lines: 24 Message-ID: <86ei40xucx.fsf@mgm.protecting.net> References: <87y629rl34.fsf@kolob.subpolar.dyndns.org> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 212.126.222.122 X-Archive: encrypt User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.2 (gnu/linux) Cancel-Lock: sha1:4GrPyIrPn5JZmhk/IUre/mODX6Q= X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.735 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_NUMERIC_HELO=1.164, T_RP_MATCHES_RCVD=-0.01, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201105/95 X-Sequence-Number: 35379 In article , Jasen Betts writes: > On 2011-05-14, Seb wrote: >> Hi, >> >> This probably reflects my confusion with how self joins work. >> >> Suppose we have this table: >> If I want to get a table with records where none of the values in column >> b are found in column a, I thought this should do it: > use the "NOT IN" operator with a subquery to retch the disallowed > values. > select * from tmp where a NOT IN (select b from tmp); The third way is an OUTER JOIN: SELECT t1.a, t1.b FROM tmp t1 LEFT JOIN tmp t2 ON t2.b = t1.a WHERE t2.b IS NULL;