SQL - The Danger of NULLs

Following up on the previous article SQL – The Power of NULLs, here are some of the things to keep in mind when you do decide to use NULL in your work.

By far, one of the most common mistakes is to assume that <> (not equal) or even = (equal) logic covers NULLs. That is completely incorrect in both cases; NULL is neither equal to anything nor is it not equal to anything, including itself. Consider the following code:

[syntax_prettify]SELECT CASE WHEN 1<>NULL THEN 1 WHEN NULL=NULL THEN 0 END[/syntax_prettify]

Of course the end result will be neither- NULL :)

What about this one: will WHEN help us be able to do this comparison on the fly?

[syntax_prettify]SELECT CASE NULL WHEN NULL THEN 1 ELSE 0 END[/syntax_prettify]

Just a little- the ELSE logic will kick in covering “all other” possibilities, but the CASE logic still relies on = and <> just the same way- NULL is still nothing, it still never rings TRUE.

So what are we left with? Well, you can always try using ISNULL (the great counterpart of NULLIF) and COALESCE in more advanced cases. Either way, remember- NULLs can help you do really interesting things with your DB and your queries, you just need to be careful, that’s all.

SQL - The Power of NULLs

Some people despise NULLs for their unusual behavior and apparent complexity, while some remain suspicious simply because of a lack of experience with them. Although at times it is not a good idea to use them in database design, they often come in handy in queries alone. They let us easily test for the “third case”- complete lack of data. Assume you have a bit column that only allows 0 and 1, so you can distinguish if something is “on” or “off”, what better way than the NULL is there for you to test for the row’s presence, or vice versa?..

[sql]SELECT *
FROM
BitTable
WHERE
BitColumn IS NOT NULL[/sql]

This approach can also be easily extended to filter vast chunks of data. Suppose you have a big table of users and a table of their last logins (i.e. audit). By left joining the latter and checking for the absent rows, you can say which users haven?t logged-in in the last 3 weeks (or never logged-in for that matter):

[sql]SELECT *
FROM
Users
LEFT JOIN AuditUsers
ON AuditUsers.UserID = Users.ID
AND ChangeDate > DATEADD(week, -3, GETDATE())
WHERE
AuditUsers.ID IS NULL[/sql]

As you can see, the possibilities are really endless. Here is another neat trick that probably deserves an article just by itself. I actually use this more often than I would like, but it does allow me to easily set “preferences” in the data output (or filter it altogether) based on seemingly unrelated data fields. All of this is done by simply having a specific CASE statement. Here is an example derived from the one above. This approach is best when used against multiple tables and columns, but this should suffice just to show how to use it. In this example we will first see users that never logged in at all, then all users who logged in over 3 weeks ago, and then all the rest. Note that you can also use TOP to limit just to the first couple of rows that come out, very handy:

[sql]SELECT *
FROM
Users
LEFT JOIN AuditUsers
ON AuditUsers.UserID = Users.ID
ORDER BY
CASE WHEN AuditUsers.ID IS NULL THEN 0
WHEN ChangeDate < DATEADD THEN 1
ELSE 2
END ASC[/sql]

Edited by Katya Pupko