?
Solved

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

Posted on 2008-06-09
3
Medium Priority
?
174 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 1000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

864 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