SQL Queries using Null fields -- Must use "is null" or "is not null"?

  • Thread starter Thread starter WWGD
  • Start date Start date
  • Tags Tags
    Fields Sql
Click For Summary

Discussion Overview

The discussion revolves around the use of "IS NULL" and "IS NOT NULL" in SQL queries, particularly when dealing with null fields. Participants explore whether these clauses are necessary based on the context of the query and the desired results.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • Some participants suggest that the necessity of using "IS NULL" or "IS NOT NULL" depends on whether null values are relevant to the results being sought.
  • One participant mentions that there is no strict requirement to use these clauses unless errors are encountered.
  • Another participant emphasizes that if nulls are to be considered in the results, "IS NULL" should be used, while "IS NOT NULL" should be used to exclude nulls.
  • A participant introduces the idea that null behavior in SQL differs from other programming languages, particularly regarding equality tests.
  • It is noted that functions operating on fields that can be null require special handling, such as using the NVL() function to avoid errors.
  • There is a discussion about the conceptual challenges that new learners face regarding null values and their behavior in SQL compared to other programming languages.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the necessity of using "IS NULL" or "IS NOT NULL," with multiple competing views presented regarding their importance based on the context of the query.

Contextual Notes

Some participants highlight that the discussion is influenced by the specific requirements of the SQL queries being constructed and the handling of null values in functions.

WWGD
Science Advisor
Homework Helper
Messages
7,798
Reaction score
13,096
Hi all,
This is for SQL queries, sorry ran out of space in the title.
Say we are doing a query that includes a null field/attribute. Must we always use either an " Is null" or
" Is not null" statement?
Thanks.
 
Computer science news on Phys.org
Depends if that factor is important for your result. I don't think there is a requirement. Are you getting errors?
 
Greg Bernhardt said:
Depends if that factor is important for your result. I don't think there is a requirement. Are you getting errors?
Hi Greg, no, I am learning the topic and the source material did not seem clear on this, i.e., on whether we should use, for the null field 'field' either "where field is null" , or " where field is not null".
 
WWGD said:
whether we should use, for the null field 'field' either "where field is null" , or " where field is not null".
Again depends on what result you are after. If you want to factor in nulls for a field then use IS NULL, if you want to factor in records that have a field that is not null, then use IS NOT NULL. If you don't care, then don't add the clause :)
 
  • Like
Likes   Reactions: WWGD
NULL is not equal to anything - including itself - what jedishrfu is telling you. When you employ a where clause you are looking for a resultset that matches what you are looking for. There may be some 'yellow' values but you do have to say '!= yellow', just say '=red' instead.

So you can ignore NULLS

Except when the select statement uses some kind of function and operates on a field that can be NULL. You cannot have a NULL in a function, so you must either exclude those rows or wrap the result in a default NVL() function to transform the NULL to something usage.
Code:
select UPPER(NVL(column_name, 'dummy')) from some_table;

In this pretend example upper will barf if column_name returns a NULL, so you throw some 'dummy' value out there to feed it a usable value when the column_name is NULL.

All indexed columns in a table should have the NOT NULL property. For the same reason.
 
  • Like
Likes   Reactions: WWGD
When people first get into computer science, they have trouble with certain special values. The concept that x = x is not necessarily true is hard to grasp, it's even worse, when that equal sign doesn't even resolve to true or false.

In most computer languages NULL is a constant, and it does equal itself. That's not true in SQL.

PHP
Code:
echo (NULL == NULL) ? 'true' : 'false';
echo (NULL === NULL) ? 'true' : 'false';
echo (NULL == 0) ? 'true' : 'false';
echo (NULL === 0) ? 'true' : 'false';
echo (NULL == 1) ? 'true' : 'false';
echo (NULL === 1) ? 'true' : 'false';
Will spit out
true
true
true
false
false
falseMySQL
Code:
SELECT NULL = NULL;
SELECT NULL IS NULL;
SELECT NULL = 0;
SELECT NULL = 1;
SELECT 0 = 0;
SELECT 0 = 1;
Will spit out
NULL
1
NULL
NULL
1
0
 
  • Like
Likes   Reactions: cellurl and WWGD

Similar threads

Replies
11
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
6
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 51 ·
2
Replies
51
Views
6K
  • · Replies 4 ·
Replies
4
Views
2K