Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple LEFT OUTER JOIN on multiple tables

Tags:

sql

oracle

I would like to convert the following Oracle SQL query syntax (Use LEFT OUTER JOIN instead of (+)):

SELECT      *
FROM        TABLEA A, TABLEB B, TABLEC C, TABLED D
WHERE       MY_COL = @col_val
AND         A.X = B.X(+)
AND         B.Y = C.Y(+)
AND         D.Z=A.Z

Here is what I tried so far:

SELECT *
FROM TABLEA A, TABLEB B, TABLEC C, TABLED D
LEFT OUTER JOIN TABLEA A ON A.X = B.X
LEFT OUTER JOIN TABLEC C ON B.Y = C.Y
WHERE MY_COL = @col_val
AND D.Z = A.Z;

But I get the error :

"ORA-00904: "B"."X" : invalid identifier"

like image 744
Platus Avatar asked Feb 12 '18 15:02

Platus


2 Answers

The join on D is an inner join, the rest are left outer joins:

SELECT *
FROM TABLEA A JOIN
     TABLED D
     ON D.Z = A.Z LEFT JOIN
     TABLEB B
     ON A.X = B.X LEFT JOIN
     TABLEC C
     ON B.Y = C.Y 
WHERE MY_COL = @col_val;

I always start chains of joins with inner joins followed by the left outer join. I never use right join, and full join rather rarely. The inner joins define the rows in the result set, so they come first.

like image 191
Gordon Linoff Avatar answered Sep 18 '22 18:09

Gordon Linoff


You don't should mix explicit and implicit sintax

  SELECT *
  FROM TABLEA A 
  INNER JOIN TABLEL L ON L.Z = A.Z
  LEFT OUTER JOIN TABLEB B ON A.X = B.X
  LEFT OUTER JOIN TABLEC C ON B.Y = C.Y
  WHERE A.MY_COL = @col_val

you should use inner join (or join) for TABLEL

like image 42
ScaisEdge Avatar answered Sep 18 '22 18:09

ScaisEdge