Solved

To compare uncommon values in two tables

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

13 Experts available now in Live!

Get 1:1 Help Now