Ram
Ram

Reputation: 51

How to compare two tables and select not exist records from first table in SQL

select ID,MRN,FileName from Upload$ t1
EXCEPT
Select MRN,FileName from Sheet1$ 

Here i would like to compare MRN and FileName from both table

Upvotes: 1

Views: 544

Answers (1)

Yogesh Sharma
Yogesh Sharma

Reputation: 50163

Your current EXCEPT statement is correct as you need to check records which are not exists in second table from first table.

select t1.* from Upload$ t1
LEFT OUTER JOIN Sheet1$ S 
       ON S.MRN = t1.MRN AND t1.FileName = S.FileName 
WHERE S.MRN IS NULL AND S.FileName IS NULL

So, here is alternative approach via JOINs (i.e. LEFT OUTER JOIN) to check record which are not exists in second table from first table and return not exists records from first table

Upvotes: 1

Related Questions