Thursday, 11 October 2012

Null Comparison with the IS NULL Operator

NULL values inevitably find their way into database tables. It is often required that
only those records that contain a NULL value in a specific column are sought. The
IS NULL operator selects only the rows where a specific column value is NULL.
Testing column values for equality to NULL is performed using the IS NULL
operator instead of the “is equal to” operator (=).
Consider the following query that fetches the LAST_NAME column from
the EMPLOYEES table for those rows which have NULL values stored in the
COMMISSION_PCT column:
select last_name from employees
where commission_pct is null;
This WHERE clause reads naturally and retrieves only the records which contain
NULL COMMISSION_PCT values. As Figure 3-13 shows, the query using the “is equal
to” operator does not return any rows, while the query using the IS NULL operator does.

No comments:

Post a Comment