SQL Tutorial – IS NULL and IS NOT NULL in SQL.

We have a table Country and we want to find out all the countries for which we do not have the LifeExpectancy data means the data is missing or has NULL values in it or we want to only select data for which we have the LifeExpectancy data means the data is not missing.

Solution –

To check whether a value is null we use the condition IS NULL in SQL. One thing to keep in mind that NULL in SQL is equal or not equal to anything, not even to itself so we can not use the = or != operator to check whether a value is null or not. To do that we use the IS NULL and IS NOT NULL conditions.

So, to find out all the countries which has missing values for the life expectancy column means they are null, we use LifeExpectancy IS NULL in the WHERE clause to filter all the null values.

SELECT
    Name as country,
    LifeExpectancy
FROM
    country
WHERE
    LifeExpectancy IS NULL

And if you want to only select the countries for which we have the life expectancy data means the data is not missing or is not null then we case use IS NOT NULL in the where clause.

SELECT
    Name as country,
    LifeExpectancy
FROM
    country
WHERE
    LifeExpectancy IS NOT NULL

If you like this post then please share it with others and subscribe to our blog for more SQL related posts.

Rating: 1 out of 5.

Leave a Reply