Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why in SQL NULL can't match with NULL?

I'm new to SQL concepts, while studying NULL expression I wonder why NULL can't match with NULL can anyone tell me a real world example to simply this concept?

like image 280
Joe Norman Avatar asked Oct 12 '12 07:10

Joe Norman


People also ask

Does NULL match with NULL in SQL?

NULL is false, even if that NULL is from another column.

Why NULL does not work in SQL?

By default, a column can hold NULL values. The NOT NULL constraint enforces a column to NOT accept NULL values. This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.

Can NULL be joined with NULL?

The result of a join of null with any other value is null. Because null values represent unknown or inapplicable values, Transact-SQL has no basis to match one unknown value to another. You can detect the presence of null values in a column from one of the tables being joined only by using an outer join.


2 Answers

NULL indicates an absence of a value. The designers of SQL decided that it made sense that, when asked whether A (for which we do not know its value) and B (for which we do not know its value) are equal, the answer must be UNKNOWN - they might be equal, they might not be. We do not have adequate information to decide either way.

You might want to read up on Three valued logic - the possible results of any comparison in SQL are TRUE, FALSE and UNKNOWN (mysql treats UNKNOWN and NULL as synonymous. Not all RDBMSs do)

like image 27
Damien_The_Unbeliever Avatar answered Oct 22 '22 03:10

Damien_The_Unbeliever


Rule : Not even a NULL can be equal to NULL.

A Non-Technical aspect

If you ask two girls, how old they are? may be you would hear them to refuse to answer your question, Both girls are giving you NULL as age and this doesn't mean both have similar age. So there is nothing can be equal to null.

like image 174
Vishwanath Dalvi Avatar answered Oct 22 '22 02:10

Vishwanath Dalvi