80

I have a list of e-mail addresses, some of them are in my table, some of them are not. I want to select all e-mails from that list and whether they are in the table or not.

I can get users whose mail adresses are in the table like this:
SELECT u.* FROM USERS u WHERE u.EMAIL IN ('email1', 'email2', 'email3')

But how can I select values in that list which are not exist in the table?

Moreover, how can I select like this:

E-Mail | Status
email1 | Exist  
email2 | Exist  
email3 | Not Exist  
email4 | Exist  
5
  • Do you mean T-SQL from Microsoft's SQL Server? If so, which version?
    – user743382
    Feb 2, 2012 at 13:35
  • 1
    Yes, MS SQL Server. I'm using 2005 and 2008 R2 both.
    – kubilay
    Feb 2, 2012 at 13:38
  • Okay, in that case I recommend Martin Smith's answer. I asked because even SQL Server 2000 is still used a fair amount and his answer wouldn't work on that.
    – user743382
    Feb 2, 2012 at 13:40
  • And with his edit, his answer would work even for all versions :)
    – user743382
    Feb 2, 2012 at 13:42
  • Thank you hvd, if you didn't ask, Martin may wouldn't have edited. I didn't know SQL is running commands for remote server, my Studio is 2008 but remote server is 2005 so (VALUES) didn't work but (UNION ALL) did.
    – kubilay
    Feb 2, 2012 at 13:49

6 Answers 6

108

For SQL Server 2008

SELECT email,
       CASE
         WHEN EXISTS(SELECT *
                     FROM   Users U
                     WHERE  E.email = U.email) THEN 'Exist'
         ELSE 'Not Exist'
       END AS [Status]
FROM   (VALUES('email1'),
              ('email2'),
              ('email3'),
              ('email4')) E(email)  

For previous versions you can do something similar with a derived table UNION ALL-ing the constants.

/*The SELECT list is the same as previously*/
FROM (
SELECT 'email1' UNION ALL
SELECT 'email2' UNION ALL
SELECT 'email3' UNION ALL
SELECT 'email4'
)  E(email)

Or if you want just the non-existing ones (as implied by the title) rather than the exact resultset given in the question, you can simply do this

SELECT email
FROM   (VALUES('email1'),
              ('email2'),
              ('email3'),
              ('email4')) E(email)  
EXCEPT
SELECT email
FROM Users
0
15

You need to somehow create a table with these values and then use NOT IN. This can be done with a temporary table, a CTE (Common Table Expression) or a Table Values Constructor (available in SQL-Server 2008):

SELECT email
FROM
    ( VALUES 
        ('email1')
      , ('email2')
      , ('email3')
    ) AS Checking (email)
WHERE email NOT IN 
      ( SELECT email 
        FROM Users
      ) 

The second result can be found with a LEFT JOIN or an EXISTS subquery:

SELECT email
     , CASE WHEN EXISTS ( SELECT * 
                          FROM Users u
                          WHERE u.email = Checking.email
                        ) 
            THEN 'Exists'
            ELSE 'Not exists'
       END AS status 
FROM
    ( VALUES 
        ('email1')
      , ('email2')
      , ('email3')
    ) AS Checking (email)
6
  • 1
    That gives the values in the table that are not in the list. The question asks it the other way around: the ones in the list that are not in the table.
    – user743382
    Feb 2, 2012 at 13:36
  • With the edit, it still won't be right if Users.email is nullable (and we don't know if it is)
    – user743382
    Feb 2, 2012 at 13:45
  • @hvd: You are right, we don't know if it is nullable. The query will answer correctly though. Feb 2, 2012 at 13:51
  • Your second query will, your first won't. WHERE 'a' IN ('b', NULL, 'c') is UNKNOWN rather than FALSE, and NOT UNKNOWN is still UNKNOWN. In other words if any user's email address is NULL, the first query will not return anything.
    – user743382
    Feb 2, 2012 at 13:54
  • @hvd: I know but that could be the "correct" behaviour for the OP :) Why else does he have Nulls in the column after all? Feb 2, 2012 at 14:02
2

You should have a table with the list of emails to check. Then do this query:

SELECT E.Email, CASE WHEN U.Email IS NULL THEN 'Not Exists' ELSE 'Exists' END Status
FROM EmailsToCheck E
LEFT JOIN (SELECT DISTINCT Email FROM Users) U
ON E.Email = U.Email
1
  • Thanks, I didn't want to go with long way. Martin's answer did it.
    – kubilay
    Feb 2, 2012 at 13:49
1

When you do not want to have the emails in the list that are in the database you'll can do the following:

select    u.name
        , u.EMAIL
        , a.emailadres
        , case when a.emailadres is null then 'Not exists'
               else 'Exists'
          end as 'Existence'
from      users u
          left join (          select 'email1' as emailadres
                     union all select 'email2'
                     union all select 'email3') a
            on  a.emailadres = u.EMAIL)

this way you'll get a result like

name | email  | emailadres | existence
-----|--------|------------|----------
NULL | NULL   | [email protected]    | Not exists
Jan  | [email protected] | [email protected]     | Exists

Using the IN or EXISTS operators are more heavy then the left join in this case.

Good luck :)

2
  • Thank you, your answer is late but correct, too. I'm glad I didn't have to create a table for values.
    – kubilay
    Feb 2, 2012 at 13:51
  • 2
    EXISTS will be more efficient than outer join as it can short circuit. Yours can bring back the same address multiple times if there are duplicates in Users. See Subqueries in CASE Expressions for some details as to how EXISTS is handled. Feb 2, 2012 at 13:53
0

This Should work with all SQL versions.

SELECT  E.AccessCode ,
        CASE WHEN C.AccessCode IS NOT NULL THEN 'Exist'
             ELSE 'Not Exist'
        END AS [Status]
FROM    ( SELECT    '60552' AS AccessCode
          UNION ALL
          SELECT    '80630'
          UNION ALL
          SELECT    '1611'
          UNION ALL
          SELECT    '0000'
        ) AS E
        LEFT OUTER JOIN dbo.Credentials C ON E.AccessCode = c.AccessCode
0

Use this : -- SQL Server 2008 or later

SELECT U.* 
FROM USERS AS U
Inner Join (
  SELECT   
    EMail, [Status]
  FROM
    (
      Values
        ('email1', 'Exist'),
        ('email2', 'Exist'),
        ('email3', 'Not Exist'),
        ('email4', 'Exist')
    )AS TempTableName (EMail, [Status])
  Where TempTableName.EMail IN ('email1','email2','email3')
) As TMP ON U.EMail = TMP.EMail

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.