Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between these 2 left joins in Oracle SQL? and why

SELECT * 
FROM TAB1 
LEFT JOIN TAB2 ON TAB2.ID_TAB1 = TAB1.ID 
JOIN TAB3 ON TAB3.ID = TAB2.ID_TAB3;

and

SELECT *
FROM TAB1
LEFT JOIN (SELECT *
           FROM TAB2
           JOIN TAB3 ON TAB3.ID = TAB2.ID_TAB3) T
ON T.ID_TAB1 = TAB1.ID;
like image 311
Cosmin Cosmin Avatar asked Jan 21 '26 14:01

Cosmin Cosmin


1 Answers

No, they are different - the inner join to TAB3 (after the left join to TAB2) in the first query effectively turns the left join back into an inner join.

The brackets in the second query ensure that the inner join to TAB3 is evaluated before the left join - so it remains a left join to TAB2, returning only those TAB2 records where there is a corresponding TAB3 record (otherwise, the TAB1 records are returned with corresponding NULLs).