SQL Double-Outer Join

sql servertsql

I have two tables A and B… A and B can be joined by a common column. A and B will have some rows that match on the join column. But A has some rows that B doesn't have, and B has some rows that A doesn't have.

A LEFT OUTER JOIN from A to B will give all rows in A, filling in NULLS for the B columns that couldn't be matched. A RIGHT OUTER JOIN gives all rows in B, filling in NULLS for the A columns that couldn't be matched.

I would like to receive ALL rows from both A and B, while returning NULL for the A columns on rows that are only in B, and NULL for the B columns that are only in A.

I'm know I can do this with UNION along with 1 INNER + 1 LEFT + 1 RIGHT, but is there a single SELECT query that can achieve this?

Best Answer

You can use a FULL OUTER JOIN for this.