tags:

views:

171

answers:

3

table1 (id, name)
table2 (id, name)

Query:

SELECT name   
FROM table2  
-- that are not in table1 already
+2  A: 
SELECT t1.name
FROM table1 t1
LEFT JOIN table2 t2 ON t2.name = t1.name
WHERE t2.name IS NULL
Kris
Thanks, this works!
z-boss
@z-boss: It's also the least performant on SQL Server: http://explainextended.com/2009/09/15/not-in-vs-not-exists-vs-left-join-is-null-sql-server/
OMG Ponies
Don't care in this case, but thanks for pointing out.
z-boss
+1  A: 
SELECT name
FROM table2
WHERE name NOT IN
    (SELECT name 
     FROM table1)

or

SELECT name 
FROM table2 
WHERE NOT EXISTS 
    (SELECT * 
     FROM table2 
     WHERE table1.name = table2.name)

See this question for 3 techniques to accomplish this

froadie
A: 

This is pure set theory which you can achieve with the minus operation.

select id, name from table1 minus select id, name from table2

Winter