x

Can 'NOT IN' be used more than once in an SQL query?

I have an answer set of 60 records called Table A. Table B has 30 records which are derived from Table A. Table C contains another 20 records from Table A. If I want to retrieve the remaining 10 records from Table A. Can I use NOT IN i.e. Select Account from A where Account not in (Sel Account from B) and Account not in (Sel Account from C)? or is there better way to do it?

more ▼

asked Jul 13, 2010 at 05:59 AM in Default

Michelle Ho gravatar image

Michelle Ho
53 1 1 1

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

That's just fine.

The other way to do it is using [EXCEPT][1]

SELECT Account FROM A
EXCEPT
(SELECT Account FROM B 
 UNION ALL 
 SELECT Account FROM C)
[1]: http://msdn.microsoft.com/en-us/library/ms188055.aspx
more ▼

answered Jul 13, 2010 at 06:01 AM

Matt Whitfield gravatar image

Matt Whitfield ♦♦
29.4k 61 65 87

EXCEPT/INTERSECT, somehow we're back to Set Theory :)
Jul 13, 2010 at 06:09 AM Blackhawk-17
@Matt - Welcome to 13K!
Jul 13, 2010 at 06:09 AM Blackhawk-17
Thank you.
Jul 13, 2010 at 06:15 AM Michelle Ho
@Blackhawk-17 - thanks :)
Jul 13, 2010 at 06:18 AM Matt Whitfield ♦♦
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x46

asked: Jul 13, 2010 at 05:59 AM

Seen: 522 times

Last Updated: Jul 13, 2010 at 05:59 AM