Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
SQL(Structured Query Language) is a famous programming language for creating and managing databases. Different statements and functions in SQL are used to create tables, alter data, etc. One of these popular functions is SQL NULLIF. It is used to handle NULL values in databases.
In the following blog, we will learn about the SQL NULLIF function in detail. We will also discuss its syntax and see some examples for better understanding.
NULLIF() function
The NULLIF is a conditional function in SQL. It compares two expressions; if they are equal, it will return NULL. Else, it will return the first expression. Let's learn the syntax for the NULLIF function.
The SQL NULLIF function can perform a comparison on both numeric and string expressions. It's a more advanced function with a few primary use cases. Comparing the two separate columns and detecting empty or incomplete data (i.e., NULL or ' ') in a column.
Syntax
The following is the syntax of the NULLIF function in SQL:
In the above syntax, theNULLIFfunction compares the input expressions. If the "first_expression" is not equal to the "second_expression," then the output will be "first_expression". Otherwise, the outcome will be NULL. Also, the function is case-sensitive, so 'dog' and 'Dog' are also different.
Let's understand the working of the SQL NULLIF function with the help of CASE statements. The above expression can be explained with CASE statement as follows:
CASE
WHEN first_expression = second_expressioin THEN NULL
ELSE first_expression
END
As we have discussed the syntax of the NULLIF() function above, let's now understand this syntax and write some queries for better understanding.
Example 1 (Numeric expressions)
In the following example, let's use the SQL NULLIF function to compare two numeric expressions.
Code
SELECT NULLIF(2,3) As Output1;
SELECT NULLIF(2,2) As Output2;
Output
As in the first query, the first expression, i.e., 2, is not equal to the second, i.e., 3. Therefore the output stored in Output1 was 2, i.e., the first expression. On the other hand, in the second query, the inputs were equal; therefore, NULL was stored in Output2.
Example 2 (String Expressions)
We will compare two string expressions using the SQL NULLIF function in this example.
As in the first query, "CodingNinjas" is not equal to "codingninjas." Therefore the output was "CodingNinjas," i.e., the first expression. On the other hand, in the second query, the expressions were equal; therefore, NULL was the output. This example also shows that it is case-sensitive.
Example 3 (NULLIF function with table)
In this example, let's use the SQL NULLIF function to compare data in a table. Let's first create a table to perform the comparison.
Let's create a table named "Products," which consists of the product's name, its brand name, and the number of stocks left.
CREATE TABLE Products (
SNo INT PRIMARY KEY NOT NULL,
Brand VARCHAR(50),
Name VARCHAR(50),
STOCK_Left INT
)
Now, let's add some data to the table "Products" using the insert command.
After executing the above command, the below pic shows how the table "Products" is stored in the database.
In the table, we can see in the "STOCK_Left" column that some products have 0 stock left. Therefore using the SQL NULLIF statement, we can get the information about the products left with 0 stock. We can run the following query for desired output.
SELECT * FROM Products WHERE NULLIF(STOCK_Left,0) IS NULL;
The result displayed after the above command is executed is shown below.
Note: The SQL NULLIF query we used above also retrieves the row where the STOCK_Left is NULL/undefined.
The above expression can be written in CASE statement in the following way:
SELECT
SNo,
Brand,
Name,
CASE
WHEN STOCK_Left = 0 THEN 'Out of Stock'
ELSE 'In Stock'
END AS Status
FROM Products
WHERE Status = 'Out of Stock';
The below picture shows the output for the above code.
The CASE statement creates a new column, 'Status.' If the 'Stock_Left' is equal to 0, then the 'Status' is 'Out of Stock,' or else it is 'In Stock.' Then with WHERE we display the rows where the status is 'Out of Stock.'
The NULLIF function returns a NULL value when the two input expressions are the same, and else it returns the first expression. It is used in different ways. For example, if an owner wants to check if any product is out of stock, he can compare the stock left with 0.
What is the difference between Coalesce() and NULLIF() functions?
The SQL NULLIF() function takes two expressions as input and returns the first expression if they are not equal; else returns NULL. The coalesce() takes a list of values as input, returns the first non-NULL value, and returns NULL if all values are null.
What data types can be used with NULLIF()?
The SQL NULLIF() function can be used with many data types, including numeric, string, date, time, etc. It is generally used with numeric data types to avoid division by zero errors.
Conclusion
We expect this article was insightful and that you learned something new today. In this blog, we learned about a SQL function named NULLIF(), which returns NULL when the two input expressions are the same, else returns the first expression. We discussed its syntax and rewrote it with a CASE statement for better understanding. The blog also explains some examples which help you clarify SQL NULLIF() function.
If you want to learn more about SQL functions, do visit