Comparing Column Values in T-SQL
Okay… so here's the scenario. I have two different tables in a SQL Database, where I need to compare both tables, and find all values in Table B that do not exist in Table A. There are two ways to do this. One with a JOIN, and one with a "NOT EXISTS" Clause:
Method 1:
SELECT b.[ColumnName]
FROM DBName.dbo.tblB b
LEFT OUTER JOIN DBName.dbo.tblA a
ON b.[ColumnName] = a.[ColumnName]
WHERE a.[ColumnName] IS NULL
GROUP BY b.[ColumnName];
Method 2:
SELECT b.[ColumnName]
FROM DBName.dbo.tblB b
WHERE NOT EXISTS
(
SELECT *
FROM DBName.dbo.tblA
WHERE a.[ColumnName] = b.[ColumnName]
)
GROUP BY b.[ColumnName];
I hope this helps someone! My malformed queries have been driving me nuts all week…