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
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.
SQL Server is always NULLS FIRST .
The ISNULL() function returns a specified value if the expression is NULL. If the expression is NOT NULL, this function returns the expression.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With