Query to get all records not matching in all tables.

Today, I will discuss about the scenario/Problem which i faced in current project.

Problem Statement : You have two Employee tables having data mentioned below.

Emp-1
EmpIdEmpName
1Shivani
2Gaurav
3Radhe
4Rahul
5Anil
Emp-2
EmpIdEmpName
1Shivani
6Ashok
3Radhe
7Vikram
4Rahul

Output :

EmpIdEmpName
2Gaurav
5Anil
6Ashok
7Vikram

Solution : Here, you need to use except or minus based on the database you are using and union clause. See the below query for the same.

select EmpId ,EmpName from Emp-1
except
select EmpId ,EmpName from Emp-2
union
select EmpId ,EmpName from Emp-2
except
select EmpId ,EmpName from Emp-1

Related posts