20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 3<br />

114<br />

FirstName LastName DateOfBirth<br />

Katie Smith 1977-01-09<br />

Steve Gee 1967-10-05<br />

John Jones 1952-10-05<br />

Jenny Jones 1953-08-25<br />

John Jackson 1974-05-27<br />

Jack Johnson 1945-06-09<br />

Seymour Botts 1956-10-21<br />

Susie Simons 1937-01-20<br />

Jamie Hills 1992-07-17<br />

Stuart Dales 1956-08-07<br />

William Doors 1994-05-28<br />

Doris Night 1997-05-28<br />

When the database looks at the records, it says for Catherine’s record, “Is NULL (unknown) less than<br />

January 1, 1970, or is it greater than January 1, 1970?”<br />

Well, the answer is unknown! Records that contain NULL values are always excluded from a results set.<br />

The same principle applies to any comparison and to inner joins as well. Additionally, most database<br />

systems consider unknowns equal when using an ORDER BY clause, so all NULL values are grouped<br />

together.<br />

In order to check for NULL values, you must use the IS NULL operator. To ensure that a value is not<br />

NULL, use the IS NOT NULL operator, as in the following code:<br />

SELECT FirstName, LastName, DateOfBirth<br />

FROM MemberDetails<br />

WHERE DateOfBirth ‘1970-01-01’<br />

OR DateOfBirth IS NULL;<br />

The preceding <strong>SQL</strong> returns all records, shown in the following table:<br />

FirstName LastName DateOfBirth<br />

Katie Smith 1977-01-09<br />

Susie Simons 1937-01-20<br />

John Jackson 1974-05-27<br />

Steve Gee 1967-10-05<br />

John Jones 1952-10-05

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!