Beginning Learning SQL – Post 7 – Equal To, Not Equal To and IS NULL

In the last post, we learned about WHERE and some of the other clauses and operators. In this post, we will go in more detail about EQUAL TO, NOT EQUAL TO, and IS NULL.

EQUAL TO and NOT EQUAL TO are operators that are searching for an exact value. This means that there can be no wildards (* or %). We will learn more about the percent sign wildcard when we get to “Like”. That means that if we run, on our little sample database:

SELECT *
FROM Production.Products
WHERE CategoryID = '0'

We will get zero rows back, because there is nothing in the column “CategoryID” that equals zero. It absolutely HAS to be a match on every single character in the statement. Also, keep in mind the single quote around our value.

If we ran something like this:

SELECT *
FROM Production.Products
WHERE CategoryID <> '0'

Then we basically just selected everything in that database. It’s a fairly simple set of queries.

On to IS NULL and IS NOT NULL. If you have, in your table, something that is null, you can search for using WHERE IS NULL.

For example:

SELECT custid, contactname, country
FROM Sales.Customers
WHERE region IS NULL

Will return us all the rows that have a “region” that is null. The same is true for NOT NULL:

SELECT custid, contactname, country
FROM Sales.Customers
WHERE region IS NOT NULL

I would suggest playing around with queries a little bit – though they are pretty basic, that doesn’t discount the fact that they are extremely important and used a daily basis (at least by me).