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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
select * from contacts c
where c.assigned_user_id not in (select u.id from users u where c.assigned_user_id = u.id )
where c.assigned_user_id not in (select u.id from users u where c.assigned_user_id = u.id )
ASKER
Thanks Limbeck, the query worked brilliantly.
left join users on contacts.assigned_user_id = users.id
where id is null