52 SQL - IS NULL
In SQL, it is not possible to check NULL values with comparison operators such as =, <, or <>. Instead, we use the IS NULL and IS NOT NULL (negation of NULL values) operators.
The SQL IS NULL Operator
The SQL IS NULL operator is used to check whether a value in a column is NULL. It returns true if the column value is NULL; otherwise false.
The NULL is a value that represents missing or unknown data, and the IS NULL operator allows us to filter for records that contain NULL values in a particular column.
Syntax
Following is the syntax of IS NULL operator −
SELECT column_name1, column_name2, column_name3, ... , column_nameN FROM table_name WHERE column_nameN IS NULL;
Example
Firstly, let us create a table named CUSTOMERS using the following query −
Now, insert values into this table using the INSERT statement as follows −
The table will be created as follows −
| ID | NAME | AGE | ADDRESS | SALARY |
|---|---|---|---|---|
| 1 | Ramesh | 32 | Ahmedabad | NULL |
| 2 | Khilan | 25 | NULL | 1500.00 |
| 3 | Kaushik | NULL | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | NULL |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | NULL | Hyderabad | 4500.00 |
| 7 | Muffy | 24 | NULL | 10000.00 |
IS NULL with SELECT Statement
We can use the IS NULL operator with a SELECT statement to filter the records with NULL values.
Example
In the following query, we are retrieving all the records from the CUSTOMERS table where the ADDRESS is null −
Output
On executing the above query, it will generate the output as shown below −
| ID | NAME | AGE | ADDRESS | SALARY |
|---|---|---|---|---|
| 2 | Khilan | 25 | NULL | 1500.00 |
| 7 | Muffy | 24 | NULL | 10000.00 |
IS NULL with COUNT() Function
We can also use the IS NULL operator with the COUNT() function in SQL to count the number of records with NULL values in a particular column.
Syntax
Following is the syntax of IS NULL operator with the COUNT() function −
SELECT COUNT(column_name) FROM table_name WHERE condition IS NULL;
Example
The following query returns the count of records have a blank field (NULL) in SALARY column of the CUSTOMERS table −
Output
The output produced is as shown below −
| COUNT(*) |
|---|
| 2 |
IS NULL with UPDATE Statement
We can use the UPDATE statement with the "IS NULL" operator in SQL to update records with NULL values in a particular column.
Syntax
Following is the syntax of the IS NULL operator with the UPDATE statement in SQL −
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE columnname1, columnname2, ... IS NULL;
Example
In the following query, we are updating the blank (NULL) records of the AGE column to a value of 48 −
Output
When we execute the program above, the output is obtained as follows −
Query OK, 2 rows affected (0.01 sec) Rows matched: 2 Changed: 2 Warnings: 0
Verification
To check whether the table has been updated or not, execute the SELECT query below −
The table is displayed as follows −
| ID | NAME | AGE | ADDRESS | SALARY |
|---|---|---|---|---|
| 1 | Ramesh | 32 | Ahmedabad | NULL |
| 2 | Khilan | 25 | NULL | 1500.00 |
| 3 | Kaushik | 48 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | NULL |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 48 | Hyderabad | 4500.00 |
| 7 | Muffy | 24 | NULL | 10000.00 |
IS NULL with DELETE Statement
We can also use the DELETE statement with IS NULL operator to delete records with NULL values in a particular column.
Syntax
Following is the syntax of the IS NULL operator with the DELETE statement in SQL −
DELETE FROM table_name WHERE columnname1, columnname2, ... IS NULL;
Example
In the following query, we are deleting the blank (NULL) records present in the SALARY column of CUSTOMERS table −
Output
We get the following result −
Query OK, 2 rows affected (0.01 sec)
Verification
Execute the SELECT query given below to check whether the table has been changed or not −
If we compile and run the program, the result is produced as follows −
| ID | NAME | AGE | ADDRESS | SALARY |
|---|---|---|---|---|
| 2 | Khilan | 25 | NULL | 1500.00 |
| 3 | Kaushik | NULL | Kota | 2000.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | NULL | Hyderabad | 4500.00 |
| 7 | Muffy | 24 | NULL | 10000.00 |

Comments
Post a Comment