Solved

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

Posted on 2008-06-09
3
166 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 142

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

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

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

706 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

21 Experts available now in Live!

Get 1:1 Help Now