Geek Slack

Start creating your course and become a part of GeekSlack.

My SQL Tutorial
About Lesson


MySQL NULL Functions


MySQL NULL Functions

MySQL provides several functions to work with NULL values in queries. These functions help to handle NULL values effectively and perform operations or transformations when dealing with them.

COALESCE Function:

SELECT COALESCE(column_name, 'Default Value') AS column_alias
FROM table_name;

The COALESCE function returns the first non-NULL value from a list of expressions. In this example, if the column_name is NULL, it will be replaced with ‘Default Value’.

IFNULL Function:

SELECT IFNULL(column_name, 'Default Value') AS column_alias
FROM table_name;

The IFNULL function returns the second argument if the first argument is NULL. In this example, if the column_name is NULL, it will be replaced with ‘Default Value’.

NULLIF Function:

SELECT NULLIF(expression1, expression2) AS result
FROM table_name;

The NULLIF function returns NULL if expression1 equals expression2; otherwise, it returns expression1. It can be useful for handling cases where two expressions might be equal and need to be treated as NULL.

These NULL functions provide flexibility in dealing with NULL values in MySQL queries and help ensure accurate data handling.

Join the conversation