Solved

How to select records that are in one database but not the other

Posted on 2008-06-09
3
169 Views
Last Modified: 2010-03-20
I have two tables with some data. They each share a common characteristic (a customer id field).
I would like to create a SQL statement that would select everything from table A that is not in table B.
For example if table A has the id's 1,2,3,4 and table B has the values of 1,4; I only want to get rows 2 and 3
from table A out of the query. I have looked at an inner join but it seems to only do the opposite of what I want...
How do I get distinct records from table A that are NOT in table B?
Thank's in advance.
0
Comment
Question by:jpwallen
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 21746619
it will be a variant of the LEFT OUTER JOIN. the trick is the WHERE clause:
SELECT a.*
  FROM tableA a
  LEFT OUTER JOIN tableB b
    ON a.id = b.id
 WHERE b.id IS NULL

Open in new window

0
 
LVL 4

Expert Comment

by:albuitra
ID: 21746620
select * from table A where not exists (select b.* from b where a.field = b.field )
0
 
LVL 2

Expert Comment

by:knowledge_riot
ID: 21749363
You could also try:

SELECT DISTINCT CustomerId from TableA where CustomerId not in (SELECT CustomerId from TableB)
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
backup and restore 21 30
Software for Local History Museum Library 5 51
TSQL XML Namespaces 7 24
add criteria to query in VB, Access 2003 2 10
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

828 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