|
|
Handling NULL Values
NULL values require special handling in SQL queries.
NULLrepresents an unknown value, and strictly speakingNULLis never equal toNULL. (NULLvalues are however treated as equal for the purposes ofGROUP BY,DISTINCTandUNION).Searching for NULL
The condition for selecting
NULLvalues isWHERE column IS NULLThe negated form (
WHERE column IS NOT NULL) selects values which are notNULL(i.e. values which are known).List all currencies, and their codes, where the exchange rate is not known:
SELECT currency, code FROM currencies WHERE exchange_rate IS NULL;Result:
CURRENCY CODE Armenian Drams AMD Kwanza AOA Brunei Dollars BND Francs Congolais CDF Saint Helena Pounds SHP Somali Shillings SOS Somoni TJSList all EAN codes where the producer is not known:
SELECT ean_code FROM items WHERE producer_id IS NULL;Result:
List all EAN codes issued to Llewellyn Publications, where the release date is not known:
SELECT ean_code FROM items WHERE release_date IS NULL AND PRODUCER_ID = (SELECT producer_id FROM producers WHERE producer = 'Llewellyn Publications');Result:
Null values in ALL, ANY, IN and EXISTS Queries
Null values should be treated cautiously, particularly in
ALL,ANY,INandEXISTSqueries.The result of a comparison involving
NULLis unknown, which is generally treated as false. This can lead to unexpected results.For example, neither of the following conditions are true:
<null> IN (...,null,...) <null> NOT IN (...,null,...)The first result is almost intuitive: since
NULLis not equal toNULL,NULLis not a member of a set containingNULL.But if
NULLis not a member of a set containingNULL, the second result is intuitively true.In fact, neither result is true or false: both are unknown. If
NULLvalues are involved on either side of the comparison,INandNOT INare not complementary. Similar arguments apply to queries containingALLorANY, for example:SELECT currency, code FROM currencies WHERE exchange_rate > ALL (SELECT exchange_rate FROM currencies WHERE currency LIKE 'D%');Result:
This query works as long as there are no
NULLvalues returned by the subselect. But perform the subselect against a range of currencies that contain aNULLvalue in the exchange rate, and the query results in an empty set:SELECT currency, code FROM currencies WHERE exchange_rate > ALL (SELECT exchange_rate FROM currencies WHERE currency LIKE 'A%');Moreover, the reverse query, currencies that have a lower exchange rate, also results in an empty set. A justification for this is that as long as an exchange rate is unknown, it is impossible to say whether other currency rates are greater or less.
Using Exists
It is always possible to rephrase a query using
ALL,ANYorINin terms of one usingEXISTS(with an outer reference between the selection and theEXISTScondition). This is to be recommended if theNULLindicator is to be permitted in the comparison sets, sinceNULLhandling is then written out explicitly in the query.Distinctions between queries involving
NULLcomparisons are subtle and are easily overlooked.It is essential that the aim of a query is stringently defined before the query is formulated in SQL, and that the possible effects of
NULLvalues in the search condition are considered.There are many real-life examples where the presence of
NULLhas resulted in unforeseen and sometimes misleading data retrievals. It is advisable to define all columns in the database tables asNOT NULLexcept those where unknown values have a specific meaning. In this way the risks of confusion withNULLhandling are minimized.
|
Mimer Information Technology AB Voice: +46 18 780 92 00 Fax: +46 18 780 92 40 info@mimer.se |
|
|