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 12th, 2006, 09:48 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 139
Thanks: 0
Thanked 0 Times in 0 Posts
Default difference between 2 tables

Hi,

Have 2 different tables(table A, table B) with a list of users. Now I would like to list all the users in table A that are NOT in table B. How would one make the general SQL query for that?

Thank you,

 
Old October 12th, 2006, 09:56 AM
Authorized User
 
Join Date: Mar 2006
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

select table1.user from table1 left join table2 on table1.user = table2.user where table2.user is null

 
Old October 12th, 2006, 09:57 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

There are two ways.

First, you can use a subquery to find all the users in the first table NOT IN the second, as:
Code:
SELECT User FROM TableA WHERE User NOT IN (SELECT User FROM TableB)
or, being trickier, you can use an OUTER JOIN of the two tables and select the User from the TableB columns of the result where the User is NULL:
Code:
SELECT TableA.User
  FROM TableA
  LEFT OUTER JOIN TableB ON TableA.User = TableB.User
 WHERE TableB.User IS NULL
admittedly, the second query is harder to understand.

It's not clear which is more efficient. Indeed, they may result in the same execution plan. Or not.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the difference? firblazer BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 1 June 30th, 2008 08:38 AM
Difference between / vs ./ vs ../ arulkumar ASP.NET 2.0 Basics 1 February 21st, 2007 12:19 PM
difference Magen Classic ASP Basics 2 January 29th, 2007 04:48 PM
creating tables within tables in access??? carswelljr Access 3 August 23rd, 2006 01:21 PM
What is the difference between these two? ramk_1978 SQL Language 1 January 14th, 2005 11:16 AM





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