learner1
learner1

Reputation: 116

How to compare values of different table rows in sql

I have two different tables like this...

TblDepartment : DeptId, DeptName

TblEmployee : EmpId, DeptId, DeptName

Now due to some error there are some wrong mappings in employee table of deptId and deptName (DeptName mapped with wrong DeptId)

I want to figure out those DeptIds...

Any help will be appreciated...

Upvotes: 0

Views: 97

Answers (2)

one of the best solutions for comparing two tables is using checksum like the below link :

https://www.red-gate.com/simple-talk/sql/t-sql-programming/on-comparing-tables-in-sql-server/

Upvotes: 1

forpas
forpas

Reputation: 164064

Use NOT EXISTS to get all the employees for which their DeptId and DeptName do not match (both) a department:

SELECT e.* 
FROM TblEmployee e
WHERE NOT EXISTS (
  SELECT 1 
  FROM TblDepartment d 
  WHERE d.DeptId = e.DeptId AND d.DeptName = e.DeptName
)

Upvotes: 1

Related Questions