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

avatar image

Michelle Ho
53 1 1 3

(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

 SELECT Account FROM A
 EXCEPT
 (SELECT Account FROM B 
  UNION ALL 
  SELECT Account FROM C)


more ▼

answered Jul 13, 2010 at 06:01 AM

avatar image

Matt Whitfield ♦♦
29.5k 62 66 88

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.

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:

x49

asked: Jul 13, 2010 at 05:59 AM

Seen: 654 times

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

Copyright 2016 Redgate Software. Privacy Policy