NULL Value in SQL
Learn via video course
Overview
NULL in SQL represents a column field in the table with no value. NULL is different from a zero value and from "none". NULL can be inserted in any column by using the assignment operator "=". Also, to test whether a NULL is present or not we have to use certain operators, for example, IS NULL and IS NOT NULL. NULL value in SQL signifies that the column's corresponding value is either undefined or unknown.
MySQL provides us with some functions which are intended to be used with NULL values. These functions are ISNULL() and IFNULL().
Scope of the Article
- This article defines what is NULL value in SQL, and how we can insert a NULL value in a table.
- This article also explains how to test whether a NULL value is present in the table or not.
- It also explains the functions associated with the NULL value.
What Is A NULL Value?
When we are inserting the data in the tables, it is possible that data for some fields are not available. Instead of adding garbage values to these fields, NULL is inserted in the fields.
Hence NULL represents a column field in the table with no value. NULL is not a datatype like "int", "char". Also, arithmetic operations on NULL values will result in NULL. Take this example, if we do 81 - NULL. It will result in NULL.
Let's create a customer table first and insert some sample data into the table.
While inserting the data in the table if we have to insert NULL in a column, we just enter NULL in the corresponding field.
The above query will insert the sample data into the table.
C_ID | C_NAME | C_GENDER | C_EMAIL | C_CONTACTNO |
---|---|---|---|---|
1 | Atul | Male | atul@email.com | 1234567890 |
2 | Prateek | Male | NULL | 1234567891 |
3 | Anchal | Female | anchal@email.com | NULL |
4 | Vaibhav | Male | vaibhav@email.com | 1234567895 |
5 | Taniya | Female | NULL | NULL |
Test for NULL Values
Suppose the firm is sending promotional SMS to customers and require the details of the customer where C_CONTACTNO is not equal to NULL.
The above query will return an empty table. This is because comparison operators such as =, != don't work on NULL value as NULL is a state, and we can't compare a value with a NULL. Hence we have to use certain operators to test whether a NULL is present in the table or not.
IS NULL
IS NULL operator is used to test whether a NULL is present in the specified column.
Syntax:
SELECT * FROM tableName WHERE columnName IS NULL;
The above query will check the C_CONTACTNO column and if there is a field with NULL in the column, it will display the row containing that field.
C_ID | C_NAME | C_GENDER | C_EMAIL | C_CONTACTNO |
---|---|---|---|---|
3 | Anchal | Female | anchal@email.com | NULL |
5 | Taniya | Female | NULL | NULL |
IS NOT NULL
IS NOT NULL operator is used to test for non-null values in the specified column, like for the above case where the firm will be sending the SMS.
Syntax :
SELECT * FROM tableName WHERE columnName IS NOT NULL;
The above query will check the C_CONTACTNO column and if NULL is not present in a field of the column, it will display the row containing that field.
C_ID | C_NAME | C_GENDER | C_EMAIL | C_CONTACTNO |
---|---|---|---|---|
1 | Atul | Male | atul@email.com | 1234567890 |
2 | Prateek | Male | NULL | 1234567891 |
4 | Vaibhav | Male | vaibhav@email.com | 1234567895 |
SET NULL Value in UPDATE Statement
Suppose Vaibhav reaches out to the firm and tells them that he lost the SIM Card of his contact number. So please mark the Contact Number of Vaibhav as NULL.
In the above scenario, we can use the assignment operator = to set Vaibhav's contact number as NULL.
The above query will set Vaibhav's contact number as NULL.
C_ID | C_NAME | C_GENDER | C_EMAIL | C_CONTACTNO |
---|---|---|---|---|
1 | Atul | Male | atul@email.com | 1234567890 |
2 | Prateek | Male | NULL | 1234567891 |
3 | Anchal | Female | anchal@email.com | NULL |
4 | Vaibhav | Male | vaibhav@email.com | NULL |
5 | Taniya | Female | NULL | NULL |
NULL Value Related Functions in MySQL
MySQL provides us with some functions, which are intended specifically for use with NULL values. These functions are:
- ISNULL()
- IFNULL()
ISNULL()
ISNULL() takes a single argument, it can be data like ISNULL("MySQL"), or it can be a NULL value ISNULL(NULL), or we can pass a column like ISNULL(columnName), and if this argument is NULL, it returns 1 (true), else it returns 0 (false). This function is used to check whether a field in the specified column is NULL or not.
Syntax:
ISNULL(expression)
Let's understand this with an example:
Suppose the firm wants to inform the customers whose email is not present in the table. So the firm wants the C_ID and C_NAME of the customers where C_EMAIL is NULL.
The above query will return the following output:
C_ID | C_NAME |
---|---|
2 | Prateek |
5 | Taniya |
In the query, if any field of the column C_EMAIl is NULL, it will display the C_ID and C_NAME of the corresponding field.
IFNULL()
IFNULL() is similar to the if-else statement, which works with NULL. It takes two arguments as inputs. If the first argument is NOT NULL, the function returns the first argument as an output; otherwise, if the first argument is NULL, it returns the second argument as an output.
Syntax:
IFNULL(expression,value)
Let's understand this with an example. Suppose the firm wants to have details of all the customers, and for customers having some columns as NULL in their records, it should be replaced by "Not Available" in the output. We can use the following query to do this task.
The above query will return the following output:
C_ID | C_NAME | C_GENDER | C_EMAIL | C_CONTACTNO |
---|---|---|---|---|
1 | Atul | Male | atul@email.com | 1234567890 |
2 | Prateek | Male | Not Available | 1234567891 |
3 | Anchal | Female | anchal@email.com | Not Available |
4 | Vaibhav | Male | vaibhav@email.com | 1234567895 |
5 | Taniya | Female | Not Available | Not Available |
As you can see in the query if a field in the column C_EMAIL contains a NULL, it will display "Not Available" for that field. The same goes for the C_CONTACTNO column.
Conclusion
- NULL is not a data type; it represents a column field in the table with no value.
- Comparison operators such as =,!=, etc. don't work on NULL values as NULL is a state, and we can't compare a value with a NULL. Hence operators such as IS NULL and IS NOT NULL are used to test NULL values.
- A field in the table can be SET to NULL using the assignment operator = in the UPDATE statement.
- ISNULL() function returns true if the argument is a NULL value, while IFNULL() returns the first argument if it is non-null. If the first argument is NULL, it returns the second argument.