Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "IS NOT NULL" and "NOT (field = NULL)" in these 2 queries

Tags:

sql

mysql

What is the difference between the following 2 queries?

DELETE FROM experience WHERE end IS NOT NULL;

And

DELETE FROM experience WHERE NOT( end=NULL);

First query was accepted as a correct answer but second was not.

like image 647
vyom bhatia Avatar asked Mar 18 '16 04:03

vyom bhatia


People also ask

What is the difference between is NULL and is NOT NULL?

The IS NULL condition is satisfied if the column contains a null value or if the expression cannot be evaluated because it contains one or more null values. If you use the IS NOT NULL operator, the condition is satisfied when the operand is column value that is not null, or an expression that does not evaluate to null.

Is NOT NULL and is NOT NULL SQL?

The IS NOT NULL condition is used in SQL to test for a non-NULL value. It returns TRUE if a non-NULL value is found, otherwise it returns FALSE. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.

What is NOT NULL field?

The NOT NULL constraint is used to ensure that a given column of a table is never assigned the null value. Once a NOT NULL constraint has been defined for a particular column, any insert or update operation that attempts to place a null value in that column will fail.

What is the difference between NULL and NOT NULL in MySQL?

In MySQL, 0 or NULL means false and anything else means true.


2 Answers

NULLs are a bit weird. A NULL is never equal to anything including another NULL. Further, any boolean operation against a NULL returns NULL.

The expression end IS NOT NULL will evaluate false if end is NULL, and true if end is not NULL.

The expression NOT( end=NULL) will actually always evaluate to NULL because (end = NULL) equals NULL and NOT (NULL) also equals NULL. More to the point in a WHERE clause, it will never evaluate true.

like image 71
Darwin von Corax Avatar answered Oct 11 '22 06:10

Darwin von Corax


NULL values are treated differently from other values.

NULL is used as a placeholder for unknown or inapplicable values. It is not possible to test for NULL values with comparison operators, such as =, <, or <>

You will have to use the IS NULL and IS NOT NULL operators instead.

Please refer below link for more details.

http://www.w3schools.com/sql/sql_null_values.asp

like image 36
Pushpendra Pal Avatar answered Oct 11 '22 04:10

Pushpendra Pal