Null values act as placeholders when we encounter missing information or unavailable data. These null
values are not part of any DataType. Therefore, it's a flexible data type.
When we work with databases, we may encounter situations, where we have to leave an empty column value. Then,
Let’s execute all these functions using different coding examples.
ISNULL()
functionThe ISNULL()
function is used to check whether the specified expression has null values. It returns 1
when the expression is null. Otherwise, it returns 0
.
SELECT _column(s), ISNULL(expression) FROM table_name;
expression
: It can be either a single value or a table column.The ISNULL()
function returns either 1
or 0
. If the expression contains a null value it returns 1
, otherwise 0
.
Let's understand this concept with the help of a coding example. Here, we have a School
database and a students
table to keep their record like ID or name.
-- Creating a school databaseCREATE DATABASE School;-- using school databasesUSE School;-- creating a student table in school databaseCREATE TABLE students (id INTEGER PRIMARY KEY,_name TEXT NOT NULL,gender TEXT NOT NULL,age INTEGER NULL,marks INTEGER NULL);-- inserting values in above created student tableINSERT INTO students VALUES (11, 'John', 'M', 23, 80);INSERT INTO students VALUES (22, 'Julia', 'F', 18, 50);INSERT INTO students VALUES (23, 'Hema', 'F',NULL, 90);INSERT INTO students VALUES (24, 'Jonathan','M', 20,NULL);-- querySELECT _name, ISNULL(age) as age FROM students;
School
database.School
database.students
table with multiple fields like ID, name, age, and marks. And insert data in it.ISNULL(age)
function to filter out such students
record(s) whose age attribute is NULL
.IFNULL()
functionThe IFNULL
function is used to replace the current NULL value with the new value.
As the function takes two parameters:
column_name
: The name of the column.mvalue_to_replace
: The value that is to be replaced. If the column value is not null then it returns that value. Otherwise, it replaces that NULL value with the given value.
SELECT _column(s), IFNULL(column_name, value_to_replace) FROM table_name;
Let’s understand this concept with the help of a coding example. Here, we have a School
database and a students
table to hold its record like ID or name.
-- Creating a school databaseCREATE DATABASE School;-- using school databasesUSE School;-- creating a student table in school databaseCREATE TABLE students (id INTEGER PRIMARY KEY,_name TEXT NOT NULL,gender TEXT NOT NULL,age INTEGER NULL,marks INTEGER NULL);-- inserting values in above created student tableINSERT INTO students VALUES (11, 'John', 'M', 23, 80);INSERT INTO students VALUES (22, 'Julia', 'F', 18, 50);INSERT INTO students VALUES (23, 'Hema', 'F',NULL, 90);INSERT INTO students VALUES (24, 'Jonathan','M', 20,NULL);SELECT id, _name, IFNULL(age, 0) as age FROM students;
School
database.School
database.students
table with multiple fields like ID, name, age, and marks. And insert data in it.IFNULL(age, 0)
to return the value(s) of age
attribute and replace NULL
values with 0
.COALESCE()
functionThe COALESCE()
function takes a list of arguments or parameters. It returns the first non-null value from the given column names.
SELECT _column(s), CAOLESCE(first_expression,....,nth_expression) FROM table_name;
Let’s understand this concept with the help of a coding example. Here, we have a School
database and a students
table to hold its record like ID or name.
-- Creating a school databaseCREATE DATABASE School;-- using school databasesUSE School;-- creating a student table in school databaseCREATE TABLE students (id INTEGER PRIMARY KEY,_name TEXT NOT NULL,gender TEXT NOT NULL,age INTEGER NULL,marks INTEGER NULL);-- inserting values in above created student tableINSERT INTO students VALUES (11, 'John', 'M', 23, 80);INSERT INTO students VALUES (22, 'Julia', 'F', NULL, NULL);INSERT INTO students VALUES (23, 'Hema', 'F',NULL, 90);INSERT INTO students VALUES (24, 'Jonathan','M', 20,NULL);SELECT _name, COALESCE(age, marks) as NonNULL FROM students;
School
database.School
database.students
table with multiple fields like ID, name, gender, age, and marks. And insert data in it.COALESCE(age, marks)
to get such record(s) that have non-null values from the given age
and marks
columns.NULLIF()
functionThe
NULLIF()
function takes two parameters and compares their values. If both the values are equal, then it will return NULL.
Otherwise, it will display the first value against that column.
SELECT _column(s), NULLIF(first_expression, second_expression) FROM table_name;
Let’s understand this concept with the help of a coding example. Here, we have a School
database and a students
table to hold its record like ID, name, gender, etc.
-- Creating a school databaseCREATE DATABASE School;-- using school databasesUSE School;-- creating a student table in school databaseCREATE TABLE students (id INTEGER PRIMARY KEY,_name TEXT NOT NULL,gender TEXT NOT NULL,age INTEGER NULL,marks INTEGER NULL);-- inserting values in above created student tableINSERT INTO students VALUES (11, 'John', 'M', 23, 80);INSERT INTO students VALUES (22, 'Julia', 'F', NULL, NULL);INSERT INTO students VALUES (23, 'Hema', 'F',NULL, 90);INSERT INTO students VALUES (24, 'Salopek','M', 20, NULL);SELECT _name, NULLIF(age, marks) age FROM students;
School
database.School
database.students
table with multiple fields like ID, name, gender, age, and marks. And insert data in it.NULLIF()
function to get NULL
if both age
and marks
are equal. Otherwise the first argument value is returned, that is, age
.