Using 'NOT IN' to exclude values

Sunday 22 June 2008
Thought I'd just share something that you should be aware of when using NOT IN to exclude rows in your SQL queries. I found out too late... after I'd provided in correct data!

Consider the following query:

SELECT row_id, created, last_upd, action, due_dt
FROM dbo.actions
WHERE action NOT IN ('Inbound Call', 'Outbound Call')

This query will return all rows that have not got an action value equal to Inbound Call or Outbound Call but will also exclude any rows with NULL in the action column. This is fine if you want to exlcude NULL values but be careful... I didn't!