Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does 10/NULL evaluate to null?

Tags:

sql

rdbms

In SQL , why does 10/NULL evaluate to NULL (or unknown) ? Example :

if((10/NULL) is NULL)
    DBMS_OUTPUT.PUT_LINE("Null.");

However , 1 = NULL being a COMPARISON is considered as FALSE. Shouldn't 10/NULL also be considered as FALSE ?

I am referring to SQL only . Not any DBMS in particular. And it might be a duplicate but I didn't know what keywords to put in search for this query.

like image 926
h4ck3d Avatar asked Jan 16 '23 12:01

h4ck3d


1 Answers

Shouldn't 10/NULL also be considered as FALSE?

No, because:

Any arithmetic expression containing a null always evaluates to null. For example, null added to 10 is null. In fact, all operators (except concatenation) return null when given a null operand.

Emphasis mine, taken from the Oracle manual: http://docs.oracle.com/cd/E11882_01/server.112/e26088/sql_elements005.htm#i59110

And this is required by the SQL standard.

Edit, as the question was for RDBMS in general:

SQL Server

When SET ANSI_NULLS is ON, an operator that has one or two NULL expressions returns UNKNOWN

Link to the the manual:

MySQL

An expression that contains NULL always produces a NULL value unless otherwise indicated in the documentation for a particular function or operator

Link to the manual

DB2

if either operand can be null, the result can be null, and if either is null, the result is the null value

Link to the manual:

PostgreSQL

Unfortunately I could not find such an explicit statement in the PostgreSQL manual, although I sure it behaves the same.


Warning: The "(except concatenation)" is an Oracle only and non-standard exception. (The empty string and NULL are almost identical in Oracle). Concatenating nulls gives null in all other DBMS.

like image 106
a_horse_with_no_name Avatar answered Jan 22 '23 00:01

a_horse_with_no_name