563 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
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!
This blog is part of our online SQL tutorial blog - however, Wise Owl also run SQL courses for up to 6 people.
|
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.
You can spot nulls in a set of results quite easily!
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:
These films have no information in the FilmBoxOfficeDollars field.
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.
All of these records have some sort of value in the box office column.
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:
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.
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!
Parts of this blog |
---|
|
Some other pages relevant to the above blogs include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2023. All Rights Reserved.