Skip to main content
Inspiring
Mar 27, 2013 10:41 PM UTC
Question

Exlcuding data from SQL table in query

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 is closed to new replies. Start a new post to keep the conversation going.

2 replies

Inspiring
Mar 28, 2013 4:24 PM UTC

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

Translate
ACS LLCAuthor
Inspiring
Mar 29, 2013 12:57 PM UTC

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

Translate