
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Use IFNULL Function Instead of COALESCE in MySQL
As we know that IFNULL() function will return the first argument if it is not NULL otherwise it returns the second argument. On the other hand, COALESCE() function will return first non-NULL argument. Actually, both IFNULL() and COALESCE() functions in MySQL works equivalently if the number of arguments is two only. The reason behind this is that IFNULL() function accepts only two arguments and in contrast, COALESCSE() function can accept any number of arguments.
Suppose if we want to use IFNULL() function at the place of COALESCE() function then the number of arguments must be two. Following example will demonstrate it −
mysql> Select IFNULL(NULL, 'Green'); +-----------------------+ | IFNULL(NULL, 'Green') | +-----------------------+ | Green | +-----------------------+ 1 row in set (0.00 sec) mysql> Select COALESCE(NULL, 'Green'); +-------------------------+ | COALESCE(NULL, 'Green') | +-------------------------+ | Green | +-------------------------+ 1 row in set (0.00 sec)
Advertisements