Full Outer Join

G

Guest

How do I do a full outer join on two tables? Or do I have to do a left outer
join unioned to an exception join...


Jim Thomlinson
 
K

Ken Snell [MVP]

A full outer join is done by unioning a left outer join with a right outer
join:

SELECT A.*, B.*
FROM TableA AS A LEFT JOIN
TableB AS B ON A.FieldName = B.FieldName
WHERE B.FieldName Is Null
UNION
SELECT C.*, D.*
FROM TableA AS C RIGHT JOIN
TableB AS D ON C.FieldName = D.FieldName
WHERE C.FieldName Is Null;
 
K

Ken Snell [MVP]

Actually, a slight "misspeak" in my post... your comment about doing a left
join unioned with a right join that selects only unmatched items is correct.
Usng the UNION ALL makes the query run a bit faster than just using UNION
(the latter must eliminate duplicates, and this query won't pull
duplicates).


SELECT A.*, B.*
FROM TableA AS A LEFT JOIN
TableB AS B ON A.FieldName = B.FieldName
UNION ALL
SELECT C.*, D.*
FROM TableA AS C RIGHT JOIN
TableB AS D ON C.FieldName = D.FieldName
WHERE C.FieldName Is Null;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top