SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to find non-matching rows in two tables?

The following Microsoft SQL Server T-SQL code examples demonstrate how to find non-matching rows in two tables with FULL OUTER JOIN or the EXCEPT operator:

 

-- SELECT INTO create 2 test tables

USE tempdb;

SELECT   TOP ( 400 ) ProductID,

                     ProductName = Name,

                     ListPrice,

                     StandardCost

INTO     Product1

FROM     AdventureWorks2008.Production.Product

ORDER BY NEWID()

-- (400 row(s) affected)

 

SELECT   TOP ( 400 ) ProductID,

                     ProductName = Name,

                     ListPrice,

                     StandardCost

INTO     Product2

FROM     AdventureWorks2008.Production.Product

ORDER BY NEWID()

-- (400 row(s) affected)

 

-- SQL FULL OUTER JOIN method

SELECT A.ProductID,

       A.ProductName,

       A.ListPrice,

       A.StandardCost,

       B.ProductID,

       B.ProductName,

       B.ListPrice,

       B.StandardCost

FROM   Product1 A

       FULL OUTER JOIN Product2 B

         ON A.ProductID = B.ProductID

            AND A.ProductName = B.ProductName

            AND A.ListPRice = B.ListPrice

            AND A.StandardCost = B.StandardCost

WHERE  A.ProductID IS NULL

        OR B.ProductID IS NULL

 

GO

-- (158 row(s) affected)

 

-- SQL EXCEPT operator method

(SELECT MESSAGE = 'Not in 2',

        *

 FROM   Product1

 EXCEPT

 SELECT MESSAGE = 'Not in 2',

        *

 FROM   Product2)

UNION ALL

(SELECT MESSAGE = 'Not in 1',

        *

 FROM   Product2

 EXCEPT

 SELECT MESSAGE = 'Not in 1',

        *

 FROM   Product1)

GO

-- (158 row(s) affected)

/*

Message     ProductID   ProductName ListPrice

....

Not in 2    321   Chainring Nut     0.00

Not in 2    343   Flat Washer 2     0.00

Not in 2    356   LL Grip Tape      0.00

Not in 2    357   ML Grip Tape      0.00

....

*/

DROP TABLE Product1

DROP TABLE Product2

 

Related article:

 

Using Outer Joins

 

http://www.sqlusa.com/bestpractices2005/fulljoin/

 

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.