Solved

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

Posted on 2009-07-15
4
222 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
Comment Utility
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
Comment Utility
select * from contacts
where not exists (select 1 from  users where contacts.assigned_user_id = users.id  )
0
 
LVL 31

Expert Comment

by:RiteshShah
Comment Utility
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
Comment Utility
Thanks Limbeck, the query worked brilliantly.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now