;

SQL NULL


In this tutorial, we will learn how to use IFNULL(), ISNULL(), COALESCE(), and NVL() Functions.

SQL IFNULL(), ISNULL(), COALESCE(), and NVL() Functions

All the Functions returns the specified value if the value in the specified column inside these function is NULL. In other words, All these functions are used to check the value is NULL or not, if the values are NULL, then specified value is return.

Syntax of IFNULL Function

IFNULL function is used in MySQL.

Syntax - IFNULL() Function
IFNULL(Expression, alternate_value);
Parameter Values of IFNULL() Function
Parameter Description
expression Required. The expression to test whether is NULL
alternate_value Required. The value to return if an expression is NULL

Syntax of ISNULL Function

In MSSQL, Azure SQL Database, Azure SQL Data Warehouse, Parallel Data Warehouse, ISNULL function is used.

Syntax - ISNULL() Function
ISNULL(Expression, alternate_value);
Parameter Values of ISNULL() Function
Parameter Description
expression Required. The expression to test whether is NULL
alternate_value Required. The value to return if an expression is NULL

Syntax of ISNULL Function used in MySQL

Syntax - ISNULL() Function used in MySQL
ISNULL(Expression);
Parameter Values of ISNULL function used in MySQL
Parameter Description
expression Required. The expression to test whether is NULL or not, if an expression is NULL, then ISNULL function return 1, otherwise it returns 0.

MySQL COALESCE Function

MySQL COALESCE Function return the first non-NULL value of a list or return NULL if there are no non-NULL values in the list.

Syntax of MySQL COALESCE function

Syntax - COALESCE() Function used in MySQL
COALESCE(value1,value2,value3,...valueN)
Parameter Values of MySQL COALESCE function used in MySQL
Parameter Description
val1, val2, val_n Required. The values to test.

Oracle NVL Function

NVL function is very similar to MySQL IFNULL function and SQL Server ISNULL. NVL function is used to replace NULL value to a specified value.

Syntax of NVL Function used in Oracle

Syntax - NVL Function used in Oracle
NVL(Expression,alternate_value);
Parameter Values of NVL Function used in Oracle
Parameter Description
expression Required. The expression to test whether is NULL
alternate_value Required. The value to return if an expression is NULL

Example:

Let us consider this table "Employee" for records.

Table Name: Employee

ID EmpName City Country Gender Salary
1 Shankar Delhi India male 25000
2 Sourabh Delhi India male 30000
3 Ranvijay Mumbai India male 15000
4 Kapil Noida India male 25000
5 Shalini Jaipur India female 18000
6 Rakesh Faridabad India male 23000
7 Akshay Mumbai India male 21000
8 Sarah New York US female 76000
9 Rocky Noida India male 28000

Example of MySQL IFNULL Function

For selecting records if City and Country is NULL, then it returns "n/a", otherwise it returns City and Country using IFNULL function, the query will be:

Example - MySQL IFNULL Function
SELECT ID,EmpName,IFNULL(City,'n/a'),IFNULL(Country,'n/a')
FROM Employee;
Output
ID EmpName City Country
1 Shankar Delhi India
2 Sourabh Delhi India
3 Ranvijay Mumbai India
4 Kapil n/a India
5 Shalini Jaipur India
6 Rakesh Faridabad India
7 Akshay Mumbai India
8 Sarah n/a n/a
9 Rocky Noida India

Example of SQL Server ISNULL Function

For selecting records if City and Country are NULL, then it returns "n/a", otherwise it returns City and Country using ISNULL function, the query will be:

Example - SQL Server ISNULL Function
SELECT ID,EmpName,ISNULL(City,'n/a'),ISNULL(Country,'n/a')
FROM Employee;
Output
ID EmpName City Country
1 Shankar Delhi India
2 Sourabh Delhi India
3 Ranvijay Mumbai India
4 Kapil n/a India
5 Shalini Jaipur India
6 Rakesh Faridabad India
7 Akshay Mumbai India
8 Sarah n/a n/a
9 Rocky Noida India

Example of MySQL ISNULL Function

For selecting records if City is NULL, then it returns '0', otherwise it returns '0' and Country using MySQL ISNULL function, the query will be:

Example - MySQL ISNULL Function
SELECT ISNULL(City)
FROM Employee;
Output
City
0
0
0
1
0
0
0
0
0

Example of MySQL COALESCE Function

For selecting records if City and Country are NULL, then it returns "n/a", otherwise it returns City and Country using COALESCE function, the query will be:

Example - MySQL COALESCE Function
SELECT ID,EmpName,COALESCE(City,'n/a'),COALESCE(Country,'n/a')
FROM Employee;
Output
ID EmpName City Country
1 Shankar Delhi India
2 Sourabh Delhi India
3 Ranvijay Mumbai India
4 Kapil n/a India
5 Shalini Jaipur India
6 Rakesh Faridabad India
7 Akshay Mumbai India
8 Sarah n/a n/a
9 Rocky Noida India

Example of Oracle NVLFunction

For selecting records if City and Country are NULL, then it returns "n/a", otherwise it returns City and Country using NVL function, the query will be:

Example - Oracle NVLFunction
SELECT ID,EmpName,NVL(City,'n/a'),NVL(Country,'n/a')
FROM Employee;
Output
ID EmpName City Country
1 Shankar Delhi India
2 Sourabh Delhi India
3 Ranvijay Mumbai India
4 Kapil n/a India
5 Shalini Jaipur India
6 Rakesh Faridabad India
7 Akshay Mumbai India
8 Sarah n/a n/a
9 Rocky Noida India