tags:

views:

31

answers:

2

I have two tables with identical fields that share many rows. I'd like to list all the rows in one table that cannot be matched in the other. Specifically, these two tables are two different versions of an experiment where the results differ slightly. An example is something like this:

|TableA|
--------
horse
cat
cow
table

|TableB|
--------
horse
cat
chair

I'd like to be able to see that TableA is missing chair from TableB and possibly in a different query that TableB is missing cow and table are missing from TableA.

My thought was to do some sort of outer join on all fields, then sort out the rows with nulls in them, but this seems heavy handed. Is this the way to go or is there are more elegant/efficient approach?

+4  A: 

Using NOT IN:

SELECT a.column
  FROM TABLE_A a
 WHERE a.column NOT IN (SELECT b.column
                          FROM TABLE_B b)

Using NOT EXISTS:

This is a good one if you need to compare more than one column...

SELECT a.column
  FROM TABLE_A a
 WHERE NOT EXISTS(SELECT NULL
                    FROM TABLE_B b
                   WHERE b.column = a.column)

Using LEFT JOIN/IS NULL:

   SELECT a.column
     FROM TABLE_A a
LEFT JOIN TABLE_B b ON b.column = a.column
    WHERE b.column IS NULL

Because of the table aliases, you could swap the table names without changing the rest of the query to see the opposite--rows from TABLE_B that aren't in TABLE_A.

OMG Ponies
Thanks I ended up using a version of your left join.
Rich
A: 

be conscious when you are using IN clause for NULLABLE columns

Guru