z-boss z-boss - 7 months ago 47
SQL Question

How to select all records from one table that do not exist in another table?


table1 (id, name)

table2 (id, name)


Query:

SELECT name
FROM table2
-- that are not in table1 already

Answer
SELECT t1.name
FROM table1 t1
LEFT JOIN table2 t2 ON t2.name = t1.name
WHERE t2.name IS NULL