From a77e7f4fa74a7e9ecaed50a99fc9ae88cbc5ad78 Mon Sep 17 00:00:00 2001 From: Navodhya Fernando Date: Mon, 13 Oct 2025 09:44:20 +0530 Subject: [PATCH 1/4] docs: Add essential NULL handling functions (IFNULL and COALESCE) --- .../content/018-essential-mysql-functions.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/ebook/en/content/018-essential-mysql-functions.md b/ebook/en/content/018-essential-mysql-functions.md index efa833a..1db7f36 100644 --- a/ebook/en/content/018-essential-mysql-functions.md +++ b/ebook/en/content/018-essential-mysql-functions.md @@ -189,3 +189,35 @@ SELECT TIME_TO_SEC('09:00') - TIME_TO_SEC('09:02') ``` -120 +## NULL Handling Functions + +These functions are essential for managing and substituting `NULL` (missing) values, ensuring your queries return clean and predictable data. + +### IFNULL() + +Returns the first expression if it is not `NULL`. Otherwise, it returns the second expression. This is a quick way to provide a default value for a single column. + +```sql +SELECT IFNULL(bonus, 0) FROM employees; + ``` +If bonus is NULL, the output is 0. Otherwise, it shows the bonus amount. + +### COALESCE() + +Returns the first non-NULL value from a list of expressions. It's useful when you have multiple potential sources for a value. + +```sql +SELECT COALESCE(formal_nickname, fan_alias, 'No Nickname') FROM players; + ``` +Returns the first available nickname or 'No Nickname' if all are NULL. + +```sql +SELECT COALESCE(NULL, NULL, NULL) + ``` +NULL + +```sql +SELECT COALESCE('First Value', 'Second Value', 'Third Value') + ``` +First Value + From 35ae7658134eeb53d1fea8d723ce19adc1f346ae Mon Sep 17 00:00:00 2001 From: Navodhya Fernando <142289080+Navodhya-Fernando@users.noreply.github.com> Date: Fri, 24 Oct 2025 13:30:06 +0530 Subject: [PATCH 2/4] Update ebook/en/content/018-essential-mysql-functions.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- ebook/en/content/018-essential-mysql-functions.md | 1 + 1 file changed, 1 insertion(+) diff --git a/ebook/en/content/018-essential-mysql-functions.md b/ebook/en/content/018-essential-mysql-functions.md index 1db7f36..0eced47 100644 --- a/ebook/en/content/018-essential-mysql-functions.md +++ b/ebook/en/content/018-essential-mysql-functions.md @@ -197,6 +197,7 @@ These functions are essential for managing and substituting `NULL` (missing) val Returns the first expression if it is not `NULL`. Otherwise, it returns the second expression. This is a quick way to provide a default value for a single column. +> Assume the `employees` table has a `bonus` column that may contain NULL values. ```sql SELECT IFNULL(bonus, 0) FROM employees; ``` From 3ee71eaa3949c700a3d1b09f20adb50dd35104c7 Mon Sep 17 00:00:00 2001 From: Navodhya Fernando <142289080+Navodhya-Fernando@users.noreply.github.com> Date: Fri, 24 Oct 2025 13:30:18 +0530 Subject: [PATCH 3/4] Update ebook/en/content/018-essential-mysql-functions.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- ebook/en/content/018-essential-mysql-functions.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ebook/en/content/018-essential-mysql-functions.md b/ebook/en/content/018-essential-mysql-functions.md index 0eced47..f2cfd8f 100644 --- a/ebook/en/content/018-essential-mysql-functions.md +++ b/ebook/en/content/018-essential-mysql-functions.md @@ -207,6 +207,15 @@ If bonus is NULL, the output is 0. Otherwise, it shows the bonus amount. Returns the first non-NULL value from a list of expressions. It's useful when you have multiple potential sources for a value. +Suppose you have a `players` table with columns for different types of nicknames: + +| id | formal_nickname | fan_alias | +|----|----------------|-----------| +| 1 | NULL | "Ace" | +| 2 | "The Rocket" | NULL | +| 3 | NULL | NULL | + +The following query returns the first available nickname for each player, or 'No Nickname' if all are NULL: ```sql SELECT COALESCE(formal_nickname, fan_alias, 'No Nickname') FROM players; ``` From e3b5e76d61703b3302cfc298036117cf053ad310 Mon Sep 17 00:00:00 2001 From: Navodhya Fernando <142289080+Navodhya-Fernando@users.noreply.github.com> Date: Fri, 24 Oct 2025 13:30:27 +0530 Subject: [PATCH 4/4] Update ebook/en/content/018-essential-mysql-functions.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- ebook/en/content/018-essential-mysql-functions.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/ebook/en/content/018-essential-mysql-functions.md b/ebook/en/content/018-essential-mysql-functions.md index f2cfd8f..ff625df 100644 --- a/ebook/en/content/018-essential-mysql-functions.md +++ b/ebook/en/content/018-essential-mysql-functions.md @@ -223,8 +223,6 @@ Returns the first available nickname or 'No Nickname' if all are NULL. ```sql SELECT COALESCE(NULL, NULL, NULL) - ``` -NULL ```sql SELECT COALESCE('First Value', 'Second Value', 'Third Value')