Solved

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

Posted on 2009-07-15
4
227 Views
Last Modified: 2013-11-30
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

0
Comment
Question by:Rafi001
4 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 24859642
select * from contacts
left join users on contacts.assigned_user_id = users.id  
where id is null
0
 
LVL 19

Accepted Solution

by:
Limbeck earned 250 total points
ID: 24859647
select * from contacts
where not exists (select 1 from  users where contacts.assigned_user_id = users.id  )
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24859689
select * from contacts c
where c.assigned_user_id  not in  (select u.id from  users u where c.assigned_user_id = u.id  )
0
 

Author Closing Comment

by:Rafi001
ID: 31603737
Thanks Limbeck, the query worked brilliantly.
0

Featured Post

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question