SQL: Need to remove duplicate + original record

I have two tables, each contains 1 column. The column is of computer names.  I need to create a final list where if the computer name is listed in both files, the record is removed completely (not just the duplicate but the duplicate + original.)
 

Eg.
List1
1-jps
23-unt
456-jps
765-jps

List2
756-jps
23-unt
456-jps
6-unt


Final List
1-jps
6-unt

aprilmfaraAsked:
Who is Participating?
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.

rpkhareCommented:
If you have set a cascade relationship among tables than, deleting record from one will automatically delete from the other. Execute this query:

Delete from List1 where ComputerName In (Select ComputerName From List2)

I would recommend you to set a cascaded (Delete).
0
rpkhareCommented:
Another option is to create a trigger like this:

Create Trigger <TriggerName> After Delete On Table1
As
Delete From Table2 where ComputerName = old.ComputerName;

I am not sure about the above syntax, please check it.
0
eszaqCommented:
Try to do UNION of two queries. Roughly, something like this
select * from list1 where NOT EXISTS (select * from list2)
UNION
select * from list2 where NOT EXISTS (select * from list1)
0

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
ErnariashCommented:

Just for information the full outer join will perform better than the union...:)

--List you need
SELECT isnull(List1.column1,List2.column1) column1   FROM List1
full outer join  List2
ON List1.column1 =  List2.column1
where not (List1.column1 is NOT null and List2.column1 is NOT null)
--List you need
SELECT column1 FROM List1 where not exists ( select 1 from List2 where List1.column1 =  List2.column1 )
union 
SELECT column1 FROM List2 where not exists ( select 1 from List1 where List1.column1 =  List2.column1 )

Open in new window

0
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.