Using Criteria in SQL Queries
Part six of a six-part series of blogs

When you write queries in SQL it's immensely useful to be able to show records matching criteria that you've set. You can do this using the WHERE clause and this blog teaches you how to use it!

  1. Using Criteria in SQL Queries
  2. Criteria Using Numbers
  3. Criteria Using Text
  4. Date Criteria in SQL Server
  5. Using AND, OR and NOT in SQL Server Queries
  6. Using NULL in Criteria (this blog)

This blog is part of our online SQL tutorial blog - however, Wise Owl also run SQL courses for up to 6 people.

Posted by Andrew Gould on 16 January 2013

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Using NULL in Criteria

In SQL Server when a value has not been entered in a column for a particular record it is referred to as NULL.  There is a unique syntax for writing criteria to find these null entries.

What are NULLS

You can spot nulls in a set of results quite easily!

 

Testing Whether Something Is Null

You can find records which contain nulls by asking for those rows where a particular field IS NULL:

SELECT

FilmName

,FilmBoxOfficeDollars

FROM

tblFilm

WHERE

FilmBoxOfficeDollars IS NULL

Simple!  The results of this query are shown below:

Missing Box Office

These films have no information in the FilmBoxOfficeDollars field.

 

Testing Whether Something Is Not Null

Testing if a field is not null uses a syntax which is almost as simple as the one shown above:

SELECT

FilmName

,FilmBoxOfficeDollars

FROM

tblFilm

WHERE

FilmBoxOfficeDollars IS NOT NULL

The above query finds all of the records which have a value in the FilmBoxOfficeDollars field.

Films with values

All of these records have some sort of value in the box office column.

 

Using Equals with NULL

Occasionally, people who are new to SQL will try to use the equals sign when testing for null, as in the example shown below:

SELECT

FilmName

,FilmBoxOfficeDollars

FROM

tblFilm

WHERE

FilmBoxOfficeDollars = NULL

With SQL Server's default settings this query won't return any results, even if there are nulls in the FilmBoxOfficeDollars field.  It is possible to change this behaviour, however, by adding an extra line to the top of your query:

--Change the behaviour of NULL

SET ANSI_NULLS OFF

SELECT

FilmName

,FilmBoxOfficeDollars

FROM

tblFilm

WHERE

FilmBoxOfficeDollars = NULL

The query will now successfully find records with null in the FilmBoxOfficeDollars field, as shown below:

Null results

The query now finds nulls in the specified field.

 

It's difficult to think of a good reason for changing the standard behaviour of nulls in criteria.  In fact, according to the Microsoft website, future versions of SQL Server will no longer allow you to do this.

What's Next?

That's everything on basic criteria in SQL queries.  If you're interested in some advanced techniques involving the IN operator and want to learn about the ALL, SOME and ANY keywords, you might want to check out my blog on subqueries!

This blog has 0 threads Add post