0

I want to find only records that exist in one table, that don't exist in another table. The part I'm struggling with is that one item can have multiple variations.

Example Table one

ItemNumber     | VendorName
1              | Frito Lay
1              | Joe's Chips
1              | Chips Galore

Example Table two

Item Number    | Vendor Name
1              | Frito Lay
1              | Joe's Chips

I'm looking to return only the records that exist in table one that don't exist in table two. The tables are identical in schema btw.

The record I'm looking to return is 1, Chips Galore

I am using SQL Server 2008.

user1837575
  • 307
  • 2
  • 6
  • 14

3 Answers3

2

You can use NOT EXISTS:

SELECT *
FROM dbo.TableOne T1
WHERE NOT EXISTS(SELECT 1 FROM dbo.TableTwo
                 WHERE ItemNumber = T1.ItemNumber 
                 AND VendorName = T1.VendorName);

Another option could be using EXCEPT:

SELECT *
FROM dbo.TableOne
EXCEPT
SELECT *
FROM dbo.TableTwo;
Lamak
  • 69,480
  • 12
  • 108
  • 116
1

So do you want to return Chip's Galore out of the first table?

SELECT * FROM [Table One]
LEFT JOIN [Table Two] ON [Table One].[Item Number] = [Table Two].[Item Number]
AND [Table One].[Vendor Name] = [Table Two].[Vendor Name] 
WHERE [Table Two].[Vendor Name] IS NULL

Or using NOT EXISTS

SELECT *
FROM [Table One] Tab1
WHERE NOT EXISTS (
    SELECT 1 
    FROM [Table Two] Tab2
    WHERE Tab1.[Item Number] = Tab2.[Item Number]
    AND Tab1.[Vendor Name] = Tab2.[Vendor Name] )
JCollerton
  • 3,227
  • 2
  • 20
  • 25
0
SELECT ItemNumber, VendorName
 from Table1
except select ItemNumber, VendorName
 from Table2

This will select everything in the "first" set that is not also found in the "second" set... and it checks every specified column in every row column in doing it. (Duplicates are removed.)

Philip Kelley
  • 39,426
  • 11
  • 57
  • 92