Re: self join

From: Harald Fuchs <hari(dot)fuchs(at)gmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: self join
Date: 2011-05-15 08:04:30
Message-ID: 86ei40xucx.fsf@mgm.protecting.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

In article <iqne7u$ssa$2(at)reversiblemaps(dot)ath(dot)cx>,
Jasen Betts <jasen(at)xnet(dot)co(dot)nz> writes:

> On 2011-05-14, Seb <spluque(at)gmail(dot)com> 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;

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Jasmin Dizdarevic 2011-05-16 10:05:03 Performance of NOT IN and <> with PG 9.0.4
Previous Message Jasen Betts 2011-05-15 02:36:46 Re: self join