Left Outer Join Syntax

Left Outer Join Syntax

Post by Steve Tuckno » Sun, 15 Aug 2004 16:33:37



What is the correct syntax for the following.
I have table A joined to table B and C. I also have table A left outer
joined to tables D and E
and in turn D is joined to F

SELECT A.*,B.*,C.*,D.*,E.*,F.*
FROM A
          LEFT OUTER JOIN D
          ON A.colD      = D.colA
          LEFT OUTER JOIN E
           ON A.colE      = E.colA,
         B,
         C
WHERE A.col1 = ?
AND     A.colB = B.colA
AND     A.colC = C.colA

(I know I can move the B and C table joins and make them explicit joins
off A)

How do I include the join of table F to table D where F.colD = D.colF in
the case where 1) F is a LEFT OUTER and 2) where F is plain (INNER?)
join  

Thanks in advance

Regards,

Steve Tucknott

ReTSol Ltd

DDI: 01903 828769

 
 
 

Left Outer Join Syntax

Post by Tom La » Mon, 16 Aug 2004 00:44:24



> How do I include the join of table F to table D where F.colD = D.colF in
> the case where 1) F is a LEFT OUTER and 2) where F is plain (INNER?)
> join  

I think you just want to parenthesize the join constructs:

        (a left join (f left join d on somecondition) on somecondition)
or
        (a left join (f join d on somecondition) on somecondition)

However you need to be clear in your mind about the semantic behavior
you want before you can pick a join order, and your question certainly
didn't give enough detail for anyone to offer advice.  In either one of
the above examples, D rows that don't have a join partner in F will
disappear before they get to the A join, resulting in different results
than you had before --- that is, some A rows that were joined to D rows
would now be extended with with nulls.  If any of those rows make it to
the final output then you will see a different and probably less useful
answer.

The short form of my point is that outer joins aren't associative and so
the order in which you do them matters a lot.  The reason JOIN is
syntactically like an operator is so that you can control that ordering
through parentheses.

                        regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

               http://archives.postgresql.org

 
 
 

Left Outer Join Syntax

Post by Steve Tuckno » Mon, 16 Aug 2004 01:57:45


Tom,
Thanks for that.
I'll be testing the converted system thoroughly, so should pick up all
the anomalies that I've introduced!
I can now finish off some of the more obscure joins in the code before I
start the data import and then testing.


    > How do I include the join of table F to table D where F.colD = D.colF in
    > the case where 1) F is a LEFT OUTER and 2) where F is plain (INNER?)
    > join  

    I think you just want to parenthesize the join constructs:

        (a left join (f left join d on somecondition) on somecondition)
    or
        (a left join (f join d on somecondition) on somecondition)

    However you need to be clear in your mind about the semantic behavior
    you want before you can pick a join order, and your question certainly
    didn't give enough detail for anyone to offer advice.  In either one of
    the above examples, D rows that don't have a join partner in F will
    disappear before they get to the A join, resulting in different results
    than you had before --- that is, some A rows that were joined to D rows
    would now be extended with with nulls.  If any of those rows make it to
    the final output then you will see a different and probably less useful
    answer.

    The short form of my point is that outer joins aren't associative and so
    the order in which you do them matters a lot.  The reason JOIN is
    syntactically like an operator is so that you can control that ordering
    through parentheses.

                        regards, tom lane

    ---------------------------(end of broadcast)---------------------------
    TIP 6: Have you searched our list archives?

                   http://archives.postgresql.org

Regards,

Steve Tucknott

ReTSol Ltd

DDI: 01903 828769

 
 
 

1. Multiple LEFT OUTER JOIN Syntax?

Hello Everyone,

I am creating an ODBC query to an Access database where the first table
(Listings)
has records for every Listings.ID, but each of the 2 tables I need to join
to it (Addendums and ListingMisc ) may or may not have records with a
matching value in their corresponding field.  The following statements work
as expected for one or the other join:

cSelect := SELECT Listings.ID, Listings.Class, Listings.Type,
Addendums.Addendum FROM
Listings LEFT OUTER JOIN Addendums ON (Listings.ID = Addendums.ListingID)

or

cSelect := SELECT Listings.ID, Listings.Class, Listings.Type,
ListingMisc.ListingFeatures1 FROM Listings LEFT OUTER JOIN ListingsMisc ON
(Listings.ID = ListingMisc.ListingID)

oServer1 := SQLSelect{ cSelect, oConn1 }
oServer1:Execute( cSelect )

The above Execute works correctly for either of the cSelect values listed.

How do I combine the Joins?  The MS ODBC help file says that both lists and
nested outer joins are supported, but I can't find samples of them and all
my attempts have returned syntax errors either at the FROM level or the JOIN
level.

Help???

Lynn C. Ormond
Lucero Research

2. xemacs 20.4 colors on console.

3. LEFT OUTER JOIN how to use?

4. Problems reading ACAD 13 .dwg files?

5. LEFT OUTER JOIN & inheritance

6. What is dredger.exe

7. LEFT OUTER JOIN and Update()

8. Adobe PDF Online for Europe

9. Left Outer Join of Views?

10. LEFT OUTER JOIN problem

11. SELECT with LEFT OUTER JOIN ON

12. Left Outer Join Question

13. Left outer join with WHERE clause?