MySQL NULL Functions
MySQL NULL Functions
In MySQL, NULL represents the absence of a value. There are several functions to handle NULL values effectively in queries.
🔹 1️⃣ IFNULL()
-
Replaces
NULLwith a specified value. -
Syntax:
Example:
-
If
marksisNULL, it will return0.
🔹 2️⃣ ISNULL()
-
Checks whether a value is
NULL. -
Returns
1if NULL, otherwise0.
Example:
| name | is_marks_null |
|---|---|
| John | 0 |
| Emma | 0 |
| Raj | 1 |
🔹 3️⃣ NULLIF()
-
Returns
NULLif two expressions are equal; otherwise returns the first expression. -
Syntax:
Example:
-
If
marks = 0, it returnsNULL; otherwise, it returns the actual marks.
🔹 4️⃣ COALESCE()
-
Returns the first non-NULL value from a list of expressions.
-
Syntax:
Example:
-
If
marksisNULL, it returns50. -
Can handle multiple fallback values:
🔹 5️⃣ IF() (conditional NULL handling)
-
MySQL IF() can also handle NULL indirectly.
-
Syntax:
Example:
-
Similar effect to
IFNULL()but allows more complex conditions.
🔹 Key Points
| Function | Purpose |
|---|---|
| IFNULL() | Replace NULL with a specific value |
| ISNULL() | Check if a value is NULL |
| NULLIF() | Return NULL if two values are equal |
| COALESCE() | Return first non-NULL value from a list |
| IF() | Conditional handling of NULL values |
