Sometimes we have columns where null values are allowed. In such cases, SQL IS NULL is a very useful operator.
SQL IS NULL operator is used to check if the value of a column is null or not. The operator filters the result set based on null column value.
Let’s discuss in detail about the IS NULL operator.
SQL IS NULL
Let’s have a quick look at SQL is null operator syntax.
CopySELECT Column(s) FROM table_name WHERE column IS NULL;
Above query will give us result set where specified column value is null.
We will understand the above-mentioned syntax in more detail through some examples.
Let’s consider the following Student table for example purpose.
Scenario: Get the percentage of students whose age is null.
CopySELECT StudentPercent FROM Student WHERE StudentAge IS NULL;
Oracle Empty String is equivalent to NULL
Null is not just limited to the null keyword in Oracle database, in fact the columns who have blank value also are considered as null when using IS NULL operator. Note that these columns types should be VARCHAR and not CHAR.
Let’s consider the following Supplier table for example purpose.
Scenario: Get the name of product where supplier name is not available.
CopySELECT ProductName FROM Supplier WHERE SupplierName IS NULL;
SQL IS NOT NULL
SQL IS NOT NULL operator is used to filter the result if the column that is used in WHERE clause contains non-null values.
Let’s discuss in detail about IS NOT NULL operator.
CopySELECT Column(s) FROM table_name WHERE Column IS NOT NULL;
In the syntax above the column values that are not null will be filtered for result.
Let’s consider the earlier defined Supplier table for example purpose.
Scenario: Get the name of the product whose supplier name is not null.
CopySELECT ProductName FROM Supplier WHERE SupplierName IS NOT NULL;