Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: Select (null = null);

This question is out of a databases exam i was looking at, so it might be nothing one can ever use in a real situation...

What output does the following valid SQL statement produce? Explain your answer!

SELECT (NULL = NULL);

I can easily produce the output of the statement, which is

school=> select (null = null);
 ?column?
----------

(1 row)

in psql 8.4.11, but how and why this is the answer i don't know... I would have guessed it tries to evaluate the expression inside the brackets and comes up with true/false but apparently it doesn't.

Any hints on why it behaves like it does?

Thanks

like image 843
tannerli Avatar asked Aug 24 '12 10:08

tannerli


People also ask

Can we do NULL NULL in SQL?

The expression "NULL = NULL" evaluates to NULL, but is actually invalid in SQL; yet ORDER BY treats NULLs as equal (whatever they precede or follow "regular" values is left to DBMS vendor). The expression "x IS NOT NULL" is not equal to "NOT(x IS NULL)", as is the case in 2VL.

Is NULL or NULL in SQL?

SQL Server is always NULLS FIRST .

Is NULL () in SQL?

The ISNULL() function returns a specified value if the expression is NULL. If the expression is NOT NULL, this function returns the expression.


1 Answers

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

like image 128
Oded Avatar answered Sep 20 '22 12:09

Oded