tags:

views:

125

answers:

3

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.

+3  A: 

With NULL you have to use IS or IS NOT. The following query should work:

SELECT * FROM sample_table WHERE (age IS NOT NULL)
Click Upvote
the above query is working.!= and " is not" both are logical operator then why the answer varies?
Warrior
That's just the way MySQL is designed, Null cannot be treated like a regular string/numeric value, so it must be checked against using IS NOT Null or IS NULL to get the correct value.
Click Upvote
+2  A: 

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.

jerebear
+1  A: 

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

gbn