If I have two tables such as this:
CREATE TABLE #table1 (id INT, name VARCHAR(10))
INSERT INTO #table1 VALUES (1,'John')
INSERT INTO #table1 VALUES (2,'Alan')
INSERT INTO #table1 VALUES (3,'Dave')
INSERT INTO #table1 VALUES (4,'Fred')
CREATE TABLE #table2 (id INT, name VARCHAR(10))
INSERT INTO #table2 VALUES (1,'John')
INSERT INTO #table2 VALUES (3,'Dave')
INSERT INTO #table2 VALUES (5,'Steve')
And I want to see all rows which only appear in one of the tables, what would be the best way to go about this?
All I can think of is to either do:
SELECT * from #table1 except SELECT * FROM #table2
UNION
SELECT * from #table2 except SELECT * FROM #table1
Or something along the lines of:
SELECT id,MAX(name) as name FROM
(
SELECT *,1 as count from #table1 UNION ALL
SELECT *,1 as count from #table2
) data
group by id
HAVING SUM(count) =1
Which would return Alan,Fred and Steve in this case.
But these feel really clunky – is there a more efficient way of approaching this?
The full outer join guarantees records from both sides of the join. Whatever record that does not have in both sides (the ones you are looking for) will have
NULLin one side or in other. That’s why we filter forNULL.The
COALESCEis there to guarantee that the nonNULLvalue will be displayed.Finally, it’s worth highlighting that repetitions are detected by ID. If you want it also to be by name, you should add
nameto theJOIN. If you only want to be by name, join bynameonly. This solution (usingJOIN) gives you that flexibility.BTW, since you provided the
CREATEandINSERTcode, I actually ran them and the code above is a fully working code.