Related
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.
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.
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
Comparing a variable to null (i.e. field = NULL) is the same as assigning an unknown value to "field." IS NOT NULL checks to see if the field is null. The second NOT (end=NULL) is assigning a value of "Unknown" to field and then NOTing the result. You should not assign a variable to an unknown value.
You should not use "NOT" alone. It should be followed by "IS". So if you want the second query to work then use - DELETE FROM experience WHERE IS NOT( end=NULL);
An expression with NULL in it can never be evaluated as true except if combined with IS (but see further). As stated in the MySql docs:
You cannot use arithmetic comparison operators such as =, <, or <> to test for NULL.
Because the result of any arithmetic comparison with NULL is also NULL, you cannot obtain any meaningful results from such comparisons.
Exceptions to the Rule
Not all expressions with NULL as argument evaluate to NULL. Apart from NULL IS NULL, also the following will yield a truthy value:
NULL <=> NULL,
COALESCE(NULL, true),
IFNULL(NULL, true),
ISNULL(NULL),
1 IN (1, NULL),
INTERVAL(1, 0, 2, NULL)
CASE 1 WHEN 1 THEN TRUE WHEN NULL THEN false END
The reason that the last three do not yield NULL is that these expressions perform some form of short-circuit evaluation. So if during the evaluation from left-to-right the outcome is clear, the rest of the expression (including the NULL) is not evaluated.
But all of the following will be NULL:
NULL = NULL
CASE NULL WHEN NULL THEN TRUE END,
'test' || NULL,
GREATEST(1, NULL),
1 NOT IN (2, NULL)
Note how in the last two expressions, short-circuiting is not a possibility: all arguments must be evaluated, at least until a NULL is found.
ANSI_NULLS Setting
There is an ANSI_NULLS setting that influences the above behaviour when set to OFF. It is ON by default, and it should in fact not be altered. As stated in the docs:
Important
In a future version of SQL Server, ANSI_NULLS will always be ON and any applications that explicitly set the option to OFF will generate an error. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.
So, I don't believe it is worth discussing this setting further: don't touch it.
I have a table named 'datatablecoulmn' with the following columns.
now i want all rows where the column FkID is NULL.FkID is an integer field
i tried the following queries
SELECT * FROM `datatablecoulmn` WHERE `FkID`=NULL
SELECT * FROM `datatablecoulmn` WHERE `FkID`<1
SELECT * FROM `datatablecoulmn` WHERE `FkID`='null'
All of these returns empty rows .Any help?
In MySQL, NULL is considered as a 'missing, unknown value', as opposed to no value. Any arithmetic comparison with NULL does not return true or false, but returns NULL instead., So, NULL != 'C' returns NULL, as opposed to returning true.
Use IS NULL condition in your query and try like this
SELECT * FROM `datatablecoulmn` WHERE `FkID` IS NULL
For handling NULL values MySQL provides three operators
IS NULL: operator returns true if column value is NULL.
IS NOT NULL: operator returns true if column value is not NULL.
<=>: operator compares values, which (unlike the = operator) is true
even for two NULL values.
You can refer to these links for more
Link 1,Link 2,Link 3
You can't compare with NULL. So you gotta check for YourColumn IS NULL (or maybe YourColumn IS NOT NULL.
NULL is a value like infinity is a number. In other words, not at all. NULL is the absence of certain information.
For the same reason that NaN (not a number) in IEEE754 floating point is not equal to other instances (or even the same instance) of NaN, nothing in SQL is equal to NULL, including NULL.
That's something that may sound strange but, when you think of the purpose of NULL, that of specifying unknown or inappropriate values, it makes sense.
In order to see if a value is NULL, you have to therefore use something like:
where COLUMN_NAME is null
More details on working with NULL in MySQL can be found here.
Use something like:
SELECT * FROM `datatablecoulmn` WHERE `FkID` is NULL
NULL is a placeholder to say there is the absence of a value. Which is why you can only use IS NULL/IS NOT NULL as predicates for such situations and not = or != or <> which is used by values.
Here is another way to exclude the records with FkID is NOT NULL:
SELECT D1.*
FROM datatablecoulmn D1
WHERE NOT EXISTS (SELECT D2.*
FROM datatablecoulmn D2
WHERE D2.`FkID` IS NOT NULL)
I was writing a simple MySQL query along these lines today:
SELECT * FROM table_name WHERE column_name IS NOT NULL and column_name !='foo';
This returned the expected number of results. But I didn't love the syntax, and tried to make it more elegant:
SELECT * FROM table_name WHERE column_name NOT IN (NULL, 'foo');
Of course, that returned 0 results.
My question is this: Can you explain why a null value would not be in (NULL, 'bar')? I think it's because you can't compare NULL with NULL, at least philosophically. But why not?
Consider this:
# ruby
nil == nil
# => true
/* JavaScript */
undefined === undefined
// true
In those languages, and nil or undefined value is equal to any other nil or undefined value. Why not in SQL?
(Bonus points about close-to-the-metal implementation details of SQL, or philosophical differences in languages?)
In SQL, direct comparisons with NULL are neither true nor false, so the IN clause will not work. This is a fundamental feature1 of the ISO SQL standard.
See this Wikipedia entry:
Since Null is not a member of any data domain, it is not considered a "value", but rather a marker (or placeholder) indicating the absence of value. Because of this, comparisons with Null can never result in either True or False, but always in a third logical result, Unknown.
In this way, the concept of NULL in SQL is very different from Ruby's nil, or JavaScript's undefined. Ruby's nil is a 'value', so nil == nil is true. However, SQL's NULL is not a 'value', so NULL = NULL is unknown (but so is NULL <> NULL). For this reason, SQL provides a different operator for comparing NULL's—NULL IS NULL is true.
1: Some may disagree that this is in fact a feature.
The database structural query language SQL implements Three Valued Logic as a means of handling comparisons with NULL field content.
True
False
Unknown
The original intent of NULL in SQL was to represent missing data in a database, i.e. the assumption that an actual value exists, but that the value is not currently recorded in the database.
So comparison with UNKNOWN value gives indeterministic result which is evalauted to FALSE.
Can you explain why a null value would not be in (NULL, 'bar')?
Necause SQL NULLs are not equal to other NULLs. That's the way the SQL NULLs are defined, because the language uses three-value logic (3VL) *. In essence, NULL means "unknown", so all comparisons to it result in the unknown result - i.e. NULL. For example, the result of column_name = NULL is NULL, not false. This is the reason behind the introduction of IS NULL and IS NOT NULL operators into the SQL language.
Your first solution is correct. You could also use a less straightforward solution that converts NULLs to 'foo' before comparison, but the expression that results from this conversion requires more thinking to understand:
WHERE IFNULL(column_name, 'foo') != 'foo'
* The behavior of NULLs in SQL, especially the difference between the way they are treated during aggregation, has been a subject of a controversy. Several suggestions were made at how to "fix" this behavior, but none of them got widespread adoption because of their complexity.
In SQL, NULLs are not equal to other NULLs.
If I don't tell you my age, and you don't tell me your age, do we therefore have the same age?
No, we can't say that's a true predicate. We simply don't have enough information to say one way or the other. It's not exactly FALSE, but it's not definitely TRUE either.
Both column_name = NULL and column_name != NULL are UNKNOWN in SQL, and conditions are satisfied only of they are actually TRUE.
column_name IN (NULL, 'foo') is logically the same as (column_name = NULL) OR (column_name = 'foo').
Likewise, column_name NOT IN (NULL, 'foo') is logically the same as NOT ((column_name = NULL) OR (column_name = 'foo')), or (column_name != NULL) AND (column_name != 'foo').
Either way, the same rules about NULL comparisons apply.
It might be convenient if SQL were to automatically convert that to (column_name IS NULL) OR (column_name = 'foo') but that's not the way the language is standardly defined, for better or for worse.
IN Clause expects a value, a NULL is not a value. So You really have to put the OR with the null to get the desired result.
MySQL manual has a good read with this.
--EDIT--
To answer your comment let's compare NULL and a ZERO.
Zero is a value. It is the unique, known quantity of zero, which is used meaningfully in arithmetic/math. We can do things with zero.
Null on the other hand is a non-value, it's just a placeholder for a data value which is unknown or in other word not specified. Math can't be performed on NULL. Undefined is the other term of NULL. NULL doesn't exist so we can't do anything with it.
Null is not zero, null is not "" (empty string). Null is just a representation of an unknown piece of data.
I hope it's now clear. :)
I'm new to SQL concepts, while studying NULL expression I wonder why NULL can't match with NULL can anyone tell me a real world example to simply this concept?
Rule : Not even a NULL can be equal to NULL.
A Non-Technical aspect
If you ask two girls, how old they are? may be you would hear them to refuse to answer your question,
Both girls are giving you NULL as age and this doesn't mean both have similar age.
So there is nothing can be equal to null.
NULL indicates an absence of a value. The designers of SQL decided that it made sense that, when asked whether A (for which we do not know its value) and B (for which we do not know its value) are equal, the answer must be UNKNOWN - they might be equal, they might not be. We do not have adequate information to decide either way.
You might want to read up on Three valued logic - the possible results of any comparison in SQL are TRUE, FALSE and UNKNOWN (mysql treats UNKNOWN and NULL as synonymous. Not all RDBMSs do)
NULL is an unknown value. Therefore it makes little sense to judge NULL == NULL. That's like asking "is this unknown value equal to that unknown value" - no clue..
See why is null not equal to null false for a possibly better explaination
NULL is the absence of data in a field.
You can check NULL values with IS NULL
See IS NULL
mysql> SELECT NULL IS NULL;
+--------------+
| NULL IS NULL |
+--------------+
| 1 |
+--------------+
1 row in set (0.00 sec)
If you need to make a comparison where NULL does indeed equal NULL, you can use a pair of coalesce methods with a special default value. The easiest example is on a nullable string column.
coalesce(MiddleName,'')=coalesce(#MiddleName,'')
This comparison returns true if #MiddleName variable has a null value and the MiddleName column also has a null value. Of course, it also matches empty strings too. If that is an issue, you could change the special value to something silly, like
coalesce(MiddleName,'<NULL_DEFAULT>')=coalesce(#MiddleName,'<NULL_DEFAULT>')
You cannot use = for NULL instead you can use IS NULL
http://www.w3schools.com/sql/sql_null_values.asp
Please folllow the link
The NULL value is never true in comparison to any other value, even NULL.
To check we can use
Is Null or Not Null operators ..
Document
Correct me if 'm wrong
In SQL the WHERE clause only includes the value if the result of the expression is equal to TRUE. (This is not the same as "if the result of the expression is not FALSE")
Any binary operation in SQL that has NULL on one side evaluates to NULL (think of NULL as being a synonym for unknown)
so
Select ... Where null = null
Select ... Where field = null
Select ... Where null = field
Will all return no rows as in each case the where class evaluates to NULL which is not TRUE
Actually NULL means UNKNOWN value So how we compare two UNKNOWN values.
In addition to using IS NULL, another way to match NULL in SQL Server, is the function ISNULL, https://learn.microsoft.com/en-us/sql/t-sql/functions/isnull-transact-sql
SELECT *
FROM TABLE_A t1
INNER JOIN TABLE_B t2 ON ISNULL(t1.somecol, somevalue) = ISNULL(t2.somecol, somevalue)
where somevalue could be 'NULL' or 0 or whatever.
Considering the following table:
create table inttest (
someint INT(10) NULL DEFAULT NULL
);
When I insert some random values
insert into inttest
(someint)
values
(1),(2),(3),(1),(2),(NULL);
and execute the query
select *
from inttest
where someint != 1;
MySQL returns 2,3,2 but not the NULL value. Is this correct? Should I extend my query with OR someint IS NULL or is it a bug in my MySQL installation?
Correct. Nothing is equal to NULL - including NULL. Or more formally, the result of evaluating NULL != 1 is UNKNOWN - and WHERE clause predicates have to evaluate to TRUE.
SELECT 1 != NULL;
-- Output: NULL
Comparison operators return TRUE, FALSE or NULL.
You're expecting NULL != 1 to give you TRUE but, sensibly, you get NULL for the comparison you make. This is because comparing anything with NULL is meaningless: NULL is not a value!.
Here's a cunning trick whereby you could get the NULL in the resultset if you still really want it. The trick relies on reversing the logic, then manually excluding the NULL possibility:
SELECT * FROM `inttest`
WHERE IF(`someint` = 1, FALSE, TRUE);
A more obvious approach might be:
SELECT * FROM `inttest`
WHERE `someint` != '1'
OR `someint` IS NULL;
You have to think of NULL as meaning UNKNOWN.
In your specific case, someint <> 1, you are asking the SQL engine to filter out anything that is not a 1. Since NULL is UNKNOWN, it could be a 1 but we will never know. Because of this, the SQL engine won't include it because it's not sure that it's not a 1.