SQL: Select (null = null); SQL: Select (null = null); sql sql

SQL: Select (null = null);


NULL stands for "Unknown Value". It is not known whether a value is true or false or anything else.

So, when comparing two unknown values, what would the answer be? Is UnknownA equal to UnknownB?

Answer? Unknown...

Here is an example for SQL Server:

IF (NULL = NULL)  PRINT 'Equals'IF (NULL != NULL)  PRINT 'Not Equals'IF (NULL IS NULL)  PRINT 'IS'IF (NULL IS NOT NULL)  PRINT 'IS NOT'

The only thing that gets printed: IS


I suppose, the expected answer is NULL. That's what MySQL does. That's what PostgreSQL returns as well, actually. Probably, your SQL client just doesn't show the single row where the only returned value is a NULL.

MS SQL server doesn't have a boolean type, though, so it cannot just select a result of boolean expression. So the result in MS SQL is an error.

The point of the question is to check your understanding of NULL logic in SQL. Instead of =, you should use the IS operator when comparing to NULL (unless the ANSI_NULLS option is set to true in MySQL).