Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old October 17th, 2006, 02:27 AM
Registered User
Join Date: May 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to Fysicus Send a message via AIM to Fysicus Send a message via MSN to Fysicus Send a message via Yahoo to Fysicus
Default IN versus LEFT JOIN (differences?)

 Yesterday I was asked to check 2 tables for differences. First I tried it using the IN operaror:

        SELECT * FROM Table1 where not(Table1.ID IN (SELECT Table2.ID
                                                     FROM Table2))

I knew there were supposed to be several differences between the tables, weirdly enough using this query I didn't find any.

Afterwards I tried it using a LEFT JOIN:

        SELECT * FROM Table1 LEFT JOIN Table2
        ON Table1.ID = Table2.ID

I made a view of this query, and when I checked for cases where Table2.ID was NULL I suddenly found my differences.

Personally I'm baffled by this. Does anyone here have an explanation for this?

Logically, at least that was what I thought, my 1st query should have sufficed, but it didn't yield anything.

Please help. This may sound like a newbie question, but I really would like to know what I did wrong.

Old October 17th, 2006, 05:22 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post

Do you have NULL as a value for the ID column in Table2?

The IN clause is shorthand for an equality test of a value with a list of values. If NULL is a value in the list, the equality test will result in unknown. Thus, it is not possible to know for sure that any given value isn't in the list because of that unknown result, so no rows are returned by NOT...IN.

Welcome to three valued logic...

Jeff Mason
Custom Apps, Inc.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Order the Ages - Left/Right versus "clockwise" gmlewis BOOK: Puzzles for Programmers and Pros BOOK ISBN: 978-0-470-12168-9 0 July 24th, 2007 01:04 PM
left outer join keyvanjan Classic ASP Professional 0 February 5th, 2006 11:54 AM
Why doesn't this LEFT JOIN work? harg7769 Access 2 April 13th, 2005 04:49 AM
left join msrnivas Classic ASP Databases 2 October 15th, 2004 07:37 AM
Oracle 8i inner join and left join problem puteri_84 Oracle 2 August 19th, 2004 07:14 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.