SQL - IS NOT NULL


The IS NOT NULL query in SQL is used to fetch all the rows that contain non-null values in a column.

A NULL value in the context of the relational database model denotes an unidentified value. If we think of a theoretical way, the NULL value still points to an unknown value, but this unknown value is not equal to a value of zero or a field with empty fields.

Null value in SQL

The SQL NULL is the term used to represent a missing value. A NULL value in a table is a value in a field that appears to be blank.

A field with a NULL value is a field with no value. It is very important to understand that a NULL value is different than a zero value or a field that contains spaces. For checking null values we can use two basic operators

  • IS NULL
  • IS NOT NULL

IS NOT NULL in SQL

Retrieving the rows with non-NULL values in a column is done using the IS NOT NULL operator. When it is used, the condition is met when either the column contains a value that is not null or when the expression that comes before the IS NOT NULL does not evaluate to null.

Syntax

Following is the syntax for IS NOT NULL −

SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;

Example

Let’s consider a table named "Car" that we are going to create in our database and which contains some null values in the fields. Execute the below query to create a table

SQL> CREATE TABLE Car (  
   ID   INT              NOT NULL,
   NAME VARCHAR (20)     NOT NULL,
   OWNER  CHAR (25),
   PRICE   DECIMAL (18, 2) NOT NULL,      
   PRIMARY KEY (ID)
);

Now we are going to populate the above-created table by using the below query.

SQL> INSERT INTO Car (ID,NAME,OWNER,PRICE)
VALUES (1, 'Bmw', 'SRK', 2000.00 );

INSERT INTO Car (ID,NAME,OWNER,PRICE)
VALUES (2, 'Audi', 'RAM', 3000.00 );

INSERT INTO Car (ID,NAME,OWNER,PRICE)
VALUES (3, 'RX100', NULL, 4000.00 );

INSERT INTO Car (ID,NAME,OWNER,PRICE)
VALUES (4, 'Benz', NULL, 5000.00 );

Verification

To check whether the table is created or not, let’s execute the below query.

SQL> SELECT * FROM Car;

On executing it, it will display a table as shown below −

+----+-------+-------+---------+
| ID | NAME  | OWNER | PRICE   |
+----+-------+-------+---------+
|  1 | Bmw   | SRK   | 2000.00 |
|  2 | Audi  | RAM   | 3000.00 |
|  3 | RX100 | NULL  | 4000.00 |
|  4 | Benz  | NULL  | 5000.00 |
+----+-------+-------+---------+

IS NOT NULL with SELECT statement

We can use IS NOT NULL operator with a SELECT statement to filter rows based on whether a particular column contains a non-NULL value.

Example

In the following example, we are going to show how the IS NOT NULL condition is going to be used to select rows with non-NULL values.

SQL> SELECT *
FROM Car
WHERE OWNER IS NOT NULL;

Output

On executing the above query, it will generate an output as shown below −

+----+------+-------+---------+
| ID | NAME | OWNER | PRICE   |
+----+------+-------+---------+
|  1 | Bmw  | SRK   | 2000.00 |
|  2 | Audi | RAM   | 3000.00 |
+----+------+-------+---------+

IS NOT NULL with COUNT() function

We can also use the IS NOT NULL operator with the COUNT() function in SQL to count the number of rows that do not contain NULL values in a particular column. This function is utilized along with the SQL SELECT command.

Syntax

Following is the syntax for COUNT() function −

SELECT COUNT(column_name)
FROM table_name
WHERE condition;

Example

To determine how many rows are having non-NULL values, use the COUNT() method along with IS NOT NULL. Execute the following query that is shown below −

SQL> SELECT COUNT(*)
FROM CAR
WHERE OWNER IS NOT NULL;

Output

On executing the above query, it will generate an output as shown below −

+----------+
| COUNT(*) |
+----------+
|        2 |
+----------+

IS NOT NULL with DELETE statement

We can also use the DELETE statement with IS NOT NULL operator to delete all rows that do not contain NULL values in a particular column.

Example

In the following example, we are going to use IS NOT NULL along with the delete statement to delete the rows that contain non-NULL values. Execute the below query to delete the non-NULL values.

SQL> DELETE FROM Car
WHERE OWNER IS NOT NULL;

Verification

To check whether the non-NULL values are removed from the table, execute the following query −

SQL> SELECT * FROM Car;

On executing the above query, it will generate the following output as shown below −

+----+-------+-------+---------+
| ID | NAME  | OWNER | PRICE   |
+----+-------+-------+---------+
|  3 | RX100 | NULL  | 4000.00 |
|  4 | Benz  | NULL  | 5000.00 |
+----+-------+-------+---------+

IS NOT NULL with UPDATE statement

We can use the UPDATE statement with the "IS NOT NULL" operator in SQL to update the value of a column for all rows that contain a non-NULL value in a particular column.

Example

In this case, we are going to check how we are going to use "is not null" in an update statement. Consider the following table "Car" in our database and run the following query to see how the update statement was used.

SQL> UPDATE Car
SET OWNER = 'DHRUVA'
WHERE OWNER IS NOT NULL;

Verification

Run the below query to verify whether the table has been updated or not −

SQL> SELECT * FROM Car;

On executing the above query, it will generate the following output as shown below −

+----+-------+--------+---------+
| ID | NAME  | OWNER  | PRICE   |
+----+-------+--------+---------+
|  1 | Bmw   | DHRUVA | 2000.00 |
|  2 | Audi  | DHRUVA | 3000.00 |
|  3 | RX100 | NULL   | 4000.00 |
|  4 | Benz  | NULL   | 5000.00 |
+----+-------+--------+---------+
Advertisements