Home > Mobile >  Find values from one SQL Server database table not present in another table
Find values from one SQL Server database table not present in another table

Time:12-15

I have two tables with the following data (that I did not create nor can I control) that correlate on table1.workgroup = table2.id.

Table1

identity_id workgroup
58173 158938173
98156 670451782
41930 159381738

Table2

id name
158938173 Sales
670451782 Engineering
159381738 Support

I need to find where table1.workgroup does not have a correlating record in table2.

CodePudding user response:

SELECT workgroup
FROM TABLE_1 
  EXCEPT
SELECT id
FROM TABLE_2

CodePudding user response:

Yet another option:

SELECT Table1.workgroup
FROM      Table1
LEFT JOIN Table2 
       ON Table1.workgroup = Table2.id
WHERE Table2.id IS NULL

CodePudding user response:

select * from a.workgroup
from a inner join table2 b on a.workgroup =b.id where table2.id is null

  • Related