How do I 'subtract' sql tables?

SELECT * FROM Table1 
LEFT JOIN Table2 on Table1.id = Table2.id
WHERE Table2.id IS NULL

this should work on almost any database engine


The set operation you are looking for is called MINUS, but in SQL Server the keyword is EXCEPT

  SELECT ... // all documents
  EXCEPT
  SELECT ... // active documents

I believe that the EXCEPT set operation became available in SQL Server 2005.


Assuming there are unique IDs that correspond across the two tables:

select * from table_both b
where not exists (select * from table_active a where a.id = b.id)