MySQL: Why can't I "column_name not in (null, 'foo')"? - mysql

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. :)

Related

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

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.

Why in SQL NULL can't match with NULL?

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.

MySQL comparison with null value

I have a column called CODE in a MySQL table which can be NULL. Say I have some rows with CODE='C' which I want to ignore in my select result set. I can have either CODE=NULL or CODE!='C' in my result set.
The following query does not return a row with CODE as NULL:
SELECT * from TABLE where CODE!='C'
But this query works as expected and I know it is the right way to do it.
SELECT * from TABLE where CODE IS NULL OR CODE!='C'
My question is why does having only CODE!='C' does not return rows where CODE=NULL? Definitely 'C' is not NULL. We are comparing no value to a character here. Can someone throw some light as why it doesn't work that way?
In MySQL, NULL is considered as a 'missing, unknown value', as opposed to no value. Take a look at this MySQL Reference on NULL.
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.
Any arithmetic comparison with 'NULL' will return false. To check this in SQL:
SELECT IF(NULL=123,'true','false')
To check NULL values we need to use IS NULL & IS NOT NULL operator.
Based on my tests and the documentation here: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html
You can compare null and get a boolean result using <=>
NOTE: it looks like NOT EQ operator, but it's EQ operator
For example:
select x <=> y;
or
select #x <=> #y;
This also compares string vs null, string vs string, etc.
In SQL, the NULL value is a special value, not comparable with any other one.
The result of a direct comparison with a NULL is always NULL, although (unfortunately) you may find FALSE in some implementation.
To test a null value you should use IS NULL and IS NOT NULL.
SELECT *
FROM `table_name`
WHERE IFNULL(`column_name` != 'C', TRUE)
The specified problem can also appear in joins and the above answers aren't particularly helpful. The way I prefer to do it is by coalescing to otherwise impossible value. For example, this
select foo from bar
inner join baz on bar.x = baz.y
won't work if bar.x and baz.y are both nulls (join won't bring results). The workaround is to use e.g.
select foo from bar
inner join baz on coalesce(bar.x, -1) = coalesce(baz.y, -1)
where -1 is "impossible" value meaning it can never appear in the data set.
select * from user where application_id='1223333344' and name is null;
I use:
SELECT * from TABLE where NOT(CODE <=> 'C')

MySQL: selecting rows where a column is null

I'm having a problem where when I try to select the rows that have a NULL for a certain column, it returns an empty set. However, when I look at the table in phpMyAdmin, it says null for most of the rows.
My query looks something like this:
SELECT pid FROM planets WHERE userid = NULL
Empty set every time.
A lot of places said to make sure it's not stored as "NULL" or "null" instead of an actual value, and one said to try looking for just a space (userid = ' ') but none of these have worked. There was a suggestion to not use MyISAM and use innoDB because MyISAM has trouble storing null. I switched the table to innoDB but now I feel like the problem may be that it still isn't actually null because of the way it might convert it. I'd like to do this without having to recreate the table as innoDB or anything else, but if I have to, I can certainly try that.
SQL NULL's special, and you have to do WHERE field IS NULL, as NULL cannot be equal to anything,
including itself (ie: NULL = NULL is always false).
See Rule 3 https://en.wikipedia.org/wiki/Codd%27s_12_rules
SELECT pid FROM planets WHERE userid IS NULL
As all are given answers I want to add little more. I had also faced the same issue.
Why did your query fail? You have,
SELECT pid FROM planets WHERE userid = NULL;
This will not give you the expected result, because from mysql doc
In SQL, the NULL value is never true in comparison to any other value, even NULL. An expression that contains NULL always produces a NULL value unless otherwise indicated in the documentation for the operators and functions involved in the expression.
Emphasis mine.
To search for column values that are NULL, you cannot use an expr = NULL test. The following statement returns no rows, because expr = NULL is never true for any expression
Solution
SELECT pid FROM planets WHERE userid IS NULL;
To test for NULL, use the IS NULL and IS NOT NULL operators.
operator IS NULL tests whether a value is NULL.
operator IS NOT NULL tests whether a value is not NULL.
MySQL comparison operators
There's also a <=> operator:
SELECT pid FROM planets WHERE userid <=> NULL
Would work. The nice thing is that <=> can also be used with non-NULL values:
SELECT NULL <=> NULL yields 1.
SELECT 42 <=> 42 yields 1 as well.
See here: https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_equal-to
Info from http://w3schools.com/sql/sql_null_values.asp:
1) NULL values represent missing unknown data.
2) By default, a table column can hold NULL values.
3) NULL values are treated differently from other values
4) It is not possible to compare NULL and 0; they are not equivalent.
5) It is not possible to test for NULL values with comparison
operators, such as =, <, or <>.
6) We will have to use the IS NULL and IS NOT NULL operators instead
So in case of your problem:
SELECT pid FROM planets WHERE userid IS NULL
Had the same issue where query:
SELECT * FROM 'column' WHERE 'column' IS NULL;
returned no values.
Seems to be an issue with MyISAM and the same query on the data in InnoDB returned expected results.
Went with:
SELECT * FROM 'column' WHERE 'column' = ' ';
Returned all expected results.
SELECT pid FROM planets WHERE userid is null;
I had the same issue when converting databases from Access to MySQL (using vb.net to communicate with the database).
I needed to assess if a field (field type varchar(1)) was null.
This statement worked for my scenario:
SELECT * FROM [table name] WHERE [field name] = ''

null used with logical operator

I have a table with name,age and address.I have totally five rows of data in the table.For some rows the age is left null.I have to display all the data where age is not null.
select * from sample_table where (age !=null);
But no output is displayed and it doesn't give an error also.Please explain this.Thanks.
With NULL you have to use IS or IS NOT. The following query should work:
SELECT * FROM sample_table WHERE (age IS NOT NULL)
The explanation from MySQL
The concept of the NULL value is a common source of confusion for
newcomers to SQL, who often think that
NULL is the same thing as an empty
string ''. This is not the case.
In SQL, the NULL value is never true in comparison to any other value,
even NULL. An expression that contains
NULL always produces a NULL value
unless otherwise indicated in the
documentation for the operators and
If you want to search for column values that are NULL, you cannot use
an expr = NULL test.
To look for NULL values, you must use the IS NULL test.
You have to use IS NULL or IS NOT NULL.
You can not compare directly against NULL because it is not value (no pedants please!)
NULL on Wikipedia
MySQL, Sybase, SQL Server... it's all the same