Search code examples
sqljoinanti-join

Finding missing emails in SQL Server


I am trying to do something I've done a million times and it's not working, can anyone tell me why?

I have a table for people who sent in resumes, and it has their email address in it...

I want to find out if any of these people have NOT signed up on the web site. The aspnet_Membership table has all the people who ARE signed up on the web site.

There are 9472 job seekers, with unique email addresses.

This query produces 1793 results:

select j.email from jobseeker j
join aspnet_Membership m on j.email = m.email

This suggests that there should be 7679 (9472-1793) emails of people who are not signed up on the web site. Since 1793 of them DID match, I would expect the rest of them DON'T match... but when I do the query for that, I get nothing!

Why is this query giving me nothing???

select j.email 
from jobseeker j
where j.email not in (select email from aspnet_Membership)

I don't know how that could be not working - it basically says "show me all the emails which are IN the jobseeker table, but NOT IN the aspnet_Membership table...


Solution

  • We had a very similar problem recently where the subquery was returning null values sometimes. Then, the in statement treats null in a weird way, I think always matching the value, so if you change your query to:

    select j.email 
    from jobseeker j
    where j.email not in (select email from aspnet_Membership
                          where email is not null)
    

    it may work....