Re: [GENERAL] Simulating an outer join - Mailing list pgsql-general

From Julian Scarfe
Subject Re: [GENERAL] Simulating an outer join
Date
Msg-id 387D0B3B.7D7F3AD8@callnetuk.com
Whole thread Raw
In response to Simulating an outer join  (Bruce Momjian <pgman@candle.pha.pa.us>)
Responses Re: [GENERAL] Simulating an outer join  (Bruce Momjian <pgman@candle.pha.pa.us>)
List pgsql-general
> Bruce Momjian wrote:
> >
> > I have been thinking about how to simulate an outer join.  It seems the
> > best way is to do:
> >
> >         SELECT tab1.col1, tab2.col3
> >         FROM tab1, tab2
> >         WHERE tab1.col1 = tab2.col2
> >         UNION ALL
> >         SELECT tab1.col1, NULL
> >         FROM tab1
> >         WHERE tab1.col1 NOT IN (SELECT tab2.col2 FROM tab2)
> >
> > Comments?  I know someone was asking about this recently.

Mike Mascari wrote:
>
> I wouldn't use IN ;-)
>
> SELECT table1.key, table2.value
> FROM table1, table2
> WHERE table1.key = table2.key
> UNION ALL
> SELECT table1.key, NULL
> FROM table1 WHERE NOT EXISTS
> (SELECT table2.key FROM table2 WHERE table1.key = table2.key);

FWIW, that's exactly Joe Celko's SQL-89 workaround for OUTER JOINs in 'SQL for
Smarties'. Well in fact he uses (SELECT * FROM table2 WHERE table1.key =
table2.key) as the subquery, but I presume that's an insignificant difference.

Julian Scarfe

pgsql-general by date:

Previous
From: Julian Scarfe
Date:
Subject: Making points into paths
Next
From: Peter Eisentraut
Date:
Subject: Re: [GENERAL] identifying performance hits: how to ???