Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to select data only appearing in one of two tables?

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?

like image 534
Dibstar Avatar asked Aug 05 '11 13:08

Dibstar


People also ask

How do I SELECT data from one table is not in another table?

How to Select All Records from One Table That Do Not Exist in Another Table in SQL? We can get the records in one table that doesn't exist in another table by using NOT IN or NOT EXISTS with the subqueries including the other table in the subqueries.

What is correct to fetch only records between two tables?

The SQL intersect operator allows us to get common values between two tables or views.

How can I get data from two tables in a single query?

In SQL, to fetch data from multiple tables, the join operator is used. The join operator adds or removes rows in the virtual table that is used by SQL server to process data before the other steps of the query consume the data.


1 Answers

select coalesce(t1.id, t2.id)     id,
       coalesce(t1.name, t2.name) name
from   #table1 t1
       full outer join #table2 t2
         on t1.id = t2.id
where  t1.id is null
        or t2.id is null  

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 NULL in one side or in other. That's why we filter for NULL.

The COALESCE is there to guarantee that the non NULL value 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 name to the JOIN. If you only want to be by name, join by name only. This solution (using JOIN) gives you that flexibility.

BTW, since you provided the CREATE and INSERT code, I actually ran them and the code above is a fully working code.

like image 60
Adriano Carneiro Avatar answered Oct 20 '22 00:10

Adriano Carneiro