SQL joining three tables, join precedence

Viewed 25464

I have three tables: R, S and P.

Table R Joins with S through a foreign key; there should be at least one record in S, so I can JOIN:

SELECT
        *
    FROM 
        R
    JOIN    S ON (S.id = R.fks)

If there's no record in S then I get no rows, that's fine.

Then table S joins with P, where records is P may or may not be present and joined with S.

So I do

SELECT
        *
    FROM 
        R
    JOIN    S ON (S.id = R.fks)
    LEFT JOIN P ON (P.id = S.fkp)

What if I wanted the second JOIN to be tied to S not to R, like if I could use parentheses:

SELECT
        *
    FROM 
        R
    JOIN    (S ON (S.id = R.fks) JOIN P ON (P.id = S.fkp))

Or is that already a natural behaviour of the cartesian product between R, S and P?

4 Answers
Related