Skip to main content
Inspiring
March 27, 2013
Question

Exlcuding data from SQL table in query

  • March 27, 2013
  • 1 reply
  • 981 views

I may have been working too many hours lately, or I'm simply losing my mind, but I'm having a heck of a time with a very strange result from a query

I have a simple query that retrieves emails from a list

SELECT emaillist_email

FROM emaillist

Now let's say the above gives 50,000 records

Now take this

SELECT emaillist_block_email

FROM emaillist_block

Say that gives 5,000 records

Now put them together

SELECT emaillist_email

FROM emaillist

WHERE emaillist_email NOT IN

(

SELECT emaillist_block_email

FROM emaillist_block

)

Now unless I am losing it, I should get 45,000 records, presuming that the 5,000 are in both tables.

The issue is, I get zero records.

Any ideas?

*** I Have solved the problem after digging a little deeper. There was a NULL record in the data which had been imported from XLS causing it to throw the query out, once I removed it the correct records were returned ***

This topic has been closed for replies.

1 reply

Inspiring
March 28, 2013

While you got your answer, using "not in" slows down queries.  Something like this would be faster.

SELECT emaillist_email

FROM emaillist eml

WHERE not exists

(select *

from emaillist_block emb

where emb.emailllist_block_email = eml.emaillist_email)

Or better yet,

alter table emaillist_email add column isBlocked bit default 0

ACS LLCAuthor
Inspiring
March 29, 2013

Thanks for the reply Dan.

How does NOT EXISTS differ from NOT IN?

It appears to do the same job, and you say it's faster.. what's the catch ;-)

I can't alter the table to add the bit because the email does not always exist in both lists, and it has to be maintained separately

Mark