Solved

To compare uncommon values in two tables

Posted on 2010-11-24
3
725 Views
Last Modified: 2012-05-10
Hi,
Suppose we have two tables both have a column CODE. Now we want to know what are the values that appear in CODE column of table 1 but are not present in CODE column of table 2. What should be the TSQL to find that?
0
Comment
Question by:nkapur
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 29

Accepted Solution

by:
Nightman earned 50 total points
ID: 34208551
Give this a bash.

SELECT * FROM Table1 as t1
WHERE
  NOT EXISTS
  (
    SELECT * FROM Table2 as t2 WHERE t2.CODE=t1.CODE
  )
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 34209271
Nightman has the right of it.  I would also recommend using select 1 rather than select * in the sub-query (usually very minimal performance impact but can be substantial if Table2 has wide rows).

SELECT * FROM Table1 as t1
WHERE
  NOT EXISTS
  (
    SELECT 1 FROM Table2 as t2 WHERE t2.CODE=t1.CODE
  )
0
 
LVL 29

Expert Comment

by:Nightman
ID: 34210375
normally wilcoxon would be correct about the select *, but in reality this a boolean expression and the value of * isn't evaluated, only the condtions.

You could even do the following with no divide by zero error:

SELECT * FROM Table1 as t1
WHERE
  NOT EXISTS
  (
    SELECT 1/0 FROM Table2 as t2 WHERE t2.CODE=t1.CODE
  )
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

690 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