Solved

To compare uncommon values in two tables

Posted on 2010-11-24
3
722 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Union 2 queries to a cte (temp table perhaps) 9 49
Check ALL SP in database make sure there are no errors 17 62
monitoring configuration for SQL server DB 32 50
SQL Recursion 6 34
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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