SQL NULL functions are used to handle NULL values in a database.
These functions can be used in SELECT, UPDATE, and DELETE statements to handle NULL values in a specific way.
IS NULL
The IS NULL function is used to check if a value is NULL.
The syntax for the IS NULL function is:
SELECT column_name FROM table_name WHERE column_name IS NULL;
For example, if you have a table called “employees” with a column called “email”, you can use the IS NULL function to select all employees who do not have an email on file:
SELECT first_name, last_name FROM employees WHERE email IS NULL;
IS NOT NULL
The IS NOT NULL function is used to check if a value is not NULL.
The syntax for the IS NOT NULL function is:
SELECT column_name FROM table_name WHERE column_name IS NOT NULL;
For example, if you have a table called “employees” with a column called “email”, you can use the IS NOT NULL function to select all employees who do have an email on file:
SELECT first_name, last_name FROM employees WHERE email IS NOT NULL;
COALESCE
The COALESCE function is used to return the first non-NULL value in a list of expressions.
The syntax for the COALESCE function is:
COALESCE(expression1, expression2, ... expression_n);
For example, if you have a table called “employees” with columns called “phone” and “email”, you can use the COALESCE function to select the phone number or email of an employee:
SELECT first_name, last_name, COALESCE(phone, email) as contact
FROM employees;
NULLIF
The NULLIF function is used to return NULL if two expressions are equal.
The syntax for the NULLIF function is:
NULLIF(expression1, expression2);
For example, if you have a table called “employees” with columns called “salary” and “commission”, you can use the NULLIF function to return the commission only if it is different from the salary:
SELECT first_name, last_name, NULLIF(commission, salary) as commission
FROM employees;
NVL or IFNULL
The NVL or IFNULL function is used to replace NULL with a default value.
The syntax for the NVL or IFNULL function is:
NVL(expression, default_value) or IFNULL(expression, default_value)
For example, if you have a table called “employees” with a column called “phone”, you can use the NVL or IFNULL function to replace all NULL values in the phone column with a default value:
SELECT first_name, last_name, NVL(phone, 'N/A') as phone
FROM employees;
It is worth noting that different database systems may have different names for these functions.
In some systems, the IS NULL and IS NOT NULL functions may be called ISNULL and ISNOTNULL, respectively.
Similarly, the COALESCE function may be called IFNULL in some systems.
These are some of the most commonly used NULL functions in SQL.
By using these functions, you can effectively handle NULL values in your database and ensure that your queries return the expected results.
For instance, the IS NULL function is particularly useful when you want to find records that have missing or null values in a specific column,
SELECT * FROM orders WHERE customer_id IS NULL;
While the IS NOT NULL function can be used to find records that do have a value in a specific column.
SELECT * FROM orders WHERE customer_id IS NOT NULL;
In the same vein, the COALESCE function can be used to select the first non-NULL value from a list of expressions.
This can be useful when you want to display a default value if a specific column is NULL.
SELECT COALESCE(shipping_address, billing_address) AS address FROM orders;
The NULLIF function is useful when you want to return NULL if two expressions are equal.
SELECT NULLIF(unit_price, 0) AS price FROM products;
Finally, the NVL or IFNULL function can be used to replace NULL values with a default value, making it easier to work with data that may contain missing values.
SELECT NVL(phone, 'N/A') as phone FROM employees;
It’s important to keep in mind that while these functions can be used to handle NULL values, it’s always best practice to design your database in such a way that it doesn’t contain NULL values in the first place.
This can be achieved by using NOT NULL constraints, default values and other database design best practices.
In conclusion, SQL NULL functions are a powerful tool for managing NULL values in a database. By understanding and using these functions, you can effectively handle missing data and ensure that your queries return the expected results.