Checking for a Null value in SQL

Yesterday I fell into a very simple but dangerous SQL trap.  Can you identify it?

SELECT first_column, another_column
FROM my_table
WHERE first_column = 1
    AND (another_column >= 100 OR 
         another_column = null)

Found it?  It’s the comparison to null.  My intention was to get rows where another_column was null, however the query did not do so.  It didn’t give me any results where another_column was null, but didn’t give me any error either.  Why?

Null in SQL is not a real value.  Null means “lack of a value” or “unknown”. Anything compared to null leads to an “unknown” result. Therefore, in SQL there are actually three values conditions can evaluate to: true, false, and unknown/null. This is called three-valued logic. SQL WHERE clauses only return rows that evaluate to true, not rows that evaluate to false or “unknown”.

The proper way of checking for null is by using the IS NULL or IS NOT NULL predicates. The correct SQL query would have been as follows:

SELECT first_column, another_column
FROM my_table
WHERE first_column = 1
    AND (another_column >= 100 OR 
         another_column IS null)

So if you’re used to languages where you check for null with equal signs, remember that in SQL, you must use the special IS NULL or IS NOT NULL predicates instead of the equals sign or other comparison operators. Don’t get tripped up like I did.

If you want to read more about, Wikipedia has a detailed article on null in SQL.

Wi-Fi everywhere!

Over the past year, more and more ShawOpen Wi-Fi hotspots have been popping up everywhere around Metro Vancouver.

This is incredibly useful for Shaw customers (like me) because it’s so easy to find reliable Wi-Fi access anywhere we go.  If you’re a Shaw internet customer, you get to save several devices so that they can automatically connect to the network without having to login through the portal.

Telus is starting to form their own network as well, under the names #TELUS and #TELUSDirect.  The one advantage they have is that for Telus customers, #TELUSDirect is a secured Wi-Fi network, whereas ShawOpen is an open unsecured network.

I’m hoping that Shaw will consider providing a secure network for customers, but until then we’ll have to use our own VPN services to secure the Wi-Fi connection.

“Error: Boot loader didn’t return any data” when booting up Xen guest

Error: Boot loader didn't return any data

I have come across this error two or three times before, and each time I spend hours trying to figure out how to get my virtual machine to boot.  This blog post is just to document a fix so that I can refer back to it, and hopefully it will help people out if they’re experiencing the issue as well.

Continue reading “Error: Boot loader didn’t return any data” when booting up Xen guest

Nooner at the Nat

Yesterday, we had a company outing to a baseball game at Nat Bailey Stadium (Vancouver Canadians vs. Everett AquaSox).  I believe this was the first time I have watched a sports game in person, so it was quite exciting even though it wasn’t MLB.

The first few innings weren’t really interesting with quick outs.  Vancouver finally scored a few runs to win the game.  Watching in person has its benefits and drawbacks.  The benefit is that you get to see everyone on the field, but the drawback is that you’re confined to one viewing angle.  The TV definitely offers better angles during the pitch.

This reminded me of when I played baseball many years ago.  I played a couple years of little league baseball at the local community centre when I was in grades 2-5.  It was both a fun and frustrating time.  In my last year, I was the main pitcher on my team and that was definitely fun.  It was frustrating because I was a horrible batter.

Software developer (mobile/web). Public transportation enthusiast. Roman Catholic.