Is it because the value null means that we do not know the value and hence it is unknown whether field1 is equal or unequal to an empty string for row 2?
You are correct. Whenever you perform a comparison against NULL
, the result is NULL
.
You can think of NULL
as meaning "unknown". When NULL
is stored in a column for a record, it doesn't mean that it doesn't have a value, it means it hasn't been entered in the database.
For example, you might have a "Person" record, with a "Date of Birth" column. If the value is NULL
, it doesn't mean that person wasn't born. It just means it hasn't been entered, so, according to the database, that person's birth date is "unknown".
If you don't know the person's date of birth, you can't answer either of these questions:
Was the person born on April 1st?
Was the person not born on April 1st?
The answer to both is "unknown".
You also can't answer:
Was the person born after April 1st?
Was the person not born before April 1st?
Whenever you compare a known value against "unknown", the answer is going to be "unknown".
Further, if two people's dates of birth are both NULL
or "unknown", you also can't answer these:
Were the two people born on the same day?
Were the two people not born on the same day?
Was person one born after person two?
Was person two born after person one?
Whenever you compare an "unknown" value to another "unknown" value, the answer is "unknown".
Comparing anything against an "unknown" value yields "unknown".
You can, however, always answer the following:
Do I know the person's date of birth?
Do I not know the person's date of birth?
To ask that question in MySQL, you use IS NOT NULL
and IS NULL
.