I have two tables. I am trying to find rows in one table that do not exist in the second table based on values in two columns. (I simplified the tables to include only two columns). There are no primary / foreign keys between the two tables. It seems pretty simple, but I now have a brain block!
DDL: CREATE TABLE [dbo].[Table_1]( [firstname] [nchar](10) NULL, [lastname] [nchar](10) NULL ) CREATE TABLE [dbo].[Table_2]( [firstname] [nchar](10) NULL, [lastname] [nchar](10) NULL )
- create sample data
INSERT INTO [dbo].[Table_1]([firstname], [lastname]) SELECT N'John ', N'Doe ' UNION ALL SELECT N'John ', N'Smith ' INSERT INTO [dbo].[Table_2]([firstname], [lastname]) SELECT N'John ', N'Doe '
- My unsuccessful attempts. I expect John the blacksmith to return.
SELECT t.* FROM Table_1 AS t WHERE NOT EXISTS (SELECT t2.* FROM Table_2 AS t2 WHERE t.firstname <> t2.firstname AND t.lastname <> t2.lastname) SELECT * FROM Table_1 AS t JOIN Table_2 AS t2 ON t.firstname <> t2.firstname AND t.lastname <> t2.lastname
source share