Compare two SQL server tables with the exact same structure.

Hi Expert,

I've got two exact same tables (only there names differ).
Only there first field (employeeID) is unique.

And I'd like to extract three lists from them:

employee ID's that do exist in table1 and not in table2
employee ID's that do exist in table2 and not in table1
employee ID's that do exist in both tables but whom differ in from each other based on the other fields in the record

I've found out how to make the first two by:
SELECT * from table1 WHERE employeeID NOT IN (SELECT employeeID FROM table2)
SELECT * from table2 WHERE employeeID NOT IN (SELECT employeeID FROM table1)

But how do I show only the records employee ID's that are in both tables but where one or more of the other fields have chanced? And would it be possible to name the fields that differ in each record in these results?

CREATE TABLE table1](
	[employeeID] [char](5) NULL,
	[field1] [int] NULL,
	[field2] [nvarchar](max) NULL,
	[field3] [nvarchar](max) NULL,
	[field4] [nvarchar](max) NULL
) ON [PRIMARY]

CREATE TABLE table2](
	[employeeID] [char](5) NULL,
	[field1] [int] NULL,
	[field2] [nvarchar](max) NULL,
	[field3] [nvarchar](max) NULL,
	[field4] [nvarchar](max) NULL
) ON [PRIMARY]

Open in new window


I hope you can help,

Steynsk
LVL 1
SteynskAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
select *
from table1 as t1
inner join table2 as t2
on t2.employeeid = t1.employeeid
where t1.field1 != t2.field1
or t1.field2 != t2.field2
or t1.field3 != t2.field3
or t1.field4 != t2.field4

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
SELECT *
FROM table1
INNER JOIN table2 ON table1.employeeID = table2.employeeID
WHERE NOT (table1.field1 = table2.field1 AND
    table1.field2 = table2.field2 AND
    table1.field3 = table2.field3 AND
    table1.field4 = table2.field4)

Open in new window

Olaf DoschkeSoftware DeveloperCommented:
How about this? http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins

table1 records not in table2
SELECT A.*
FROM Table1 A
LEFT JOIN Table2 B
ON A.employeeID = B.employeeID
WHERE B.employeeID IS NULL

Open in new window


tabl2 records not in table1
SELECT B.*
FROM Table1 A
RIGHT JOIN Table2 B
ON A.employeeID = B.employeeID
WHERE A.employeeID IS NULL

Open in new window


In both tables, no matter whether equal or differing:
SELECT A.*, B.*
FROM Table1 A
INNER JOIN Table2 B
ON A.employeeID = B.employeeID 

Open in new window


In both tables, differing:
SELECT A.*, B.*
FROM (Select *, CHECKSUM(*) chk FROM Table1) A
INNER JOIN (Select *, CHECKSUM(*) chk FROM Table2) B
ON A.employeeID = B.employeeID AND A.chk != B.chk

Open in new window


Bye, Olaf.
SteynskAuthor Commented:
Excellent Thank you all for the useful answers.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.