Link to home
Start Free TrialLog in
Avatar of Rafi001
Rafi001

asked on

How to write a MS SQL query to filter data from 2 tables

Hello All,
I am new to SQL and trying to write a select query to get a result from 2 table. Table1 is CONTACTS and 2 is USERS. CONTACTS has a field named Assigned_user_ID, which should have the same value as field ID in USERS. We actually migrated from one system to this new system, due which we have records in CONTACTS with Assigned_user_id, which are not in USERS ID. These
I want to get a list of those records which do not match in both tables. I have come up with this code, which doesn't give me any results. However, it doesn't give me any error either. Please see the code.
Many Thanks
Rafi


select * from contacts 
inner join users on contacts.assigned_user_id = users.id  
where contacts.assigned_user_id != users.id

Open in new window

Avatar of momi_sabag
momi_sabag
Flag of United States of America image

select * from contacts
left join users on contacts.assigned_user_id = users.id  
where id is null
ASKER CERTIFIED SOLUTION
Avatar of Limbeck
Limbeck

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
select * from contacts c
where c.assigned_user_id  not in  (select u.id from  users u where c.assigned_user_id = u.id  )
Avatar of Rafi001
Rafi001

ASKER

Thanks Limbeck, the query worked brilliantly.