Skip to content
Advertisement

SQL ‘left outer join with 2 right tables

From the sql tutorial https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_join_left,

when the right table does not has the ‘CustomerID’ (key of the join), it will left the row values as null.

My question is for those rows does not have entries in the right table, can I try to ‘left outer join’ with a second table?

So that the final results will have some rows joined with first right table, and some with the 2nd right table?

Advertisement

Answer

You could use two joins and coalesce the columns you’re querying:

SELECT    t.id, t.col1, COALESCE(j1.joined_col, j2.joined_col)
FROM      t
LEFT JOIN j1 ON t.id = j1.id
LEFT JOIN j2 ON t.id = j2.id
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement