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 NULL with a specified value.

  • Syntax:



 

Example:



 

  • If marks is NULL, it will return 0.


🔹 2️⃣ ISNULL()

  • Checks whether a value is NULL.

  • Returns 1 if NULL, otherwise 0.



 

Example:



 

name is_marks_null
John 0
Emma 0
Raj 1

🔹 3️⃣ NULLIF()

  • Returns NULL if two expressions are equal; otherwise returns the first expression.

  • Syntax:



 

Example:



 

  • If marks = 0, it returns NULL; otherwise, it returns the actual marks.


🔹 4️⃣ COALESCE()

  • Returns the first non-NULL value from a list of expressions.

  • Syntax:



 

Example:



 

  • If marks is NULL, it returns 50.

  • Can handle multiple fallback values:

COALESCE(marks, bonus_marks, 0)

🔹 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

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *