
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can I customize the output of MySQL SUM() function to 0 instead of NULL when there are no matching rows?
As we know that the SUM() function returns NULL if there is no matching row but sometimes we want it to return zero instead of NULL. For this purpose, we can use the MySQL COALESCE() function which accepts two arguments and returns the second argument if the first argument is NULL, otherwise, it returns the first argument. To understand the above concept, consider an ‘employee_tbl’ table, which is having the following records −
mysql> SELECT * FROM employee_tbl; +------+------+------------+--------------------+ | id | name | work_date | daily_typing_pages | +------+------+------------+--------------------+ | 1 | John | 2007-01-24 | 250 | | 2 | Ram | 2007-05-27 | 220 | | 3 | Jack | 2007-05-06 | 170 | | 3 | Jack | 2007-04-06 | 100 | | 4 | Jill | 2007-04-06 | 220 | | 5 | Zara | 2007-06-06 | 300 | | 5 | Zara | 2007-02-06 | 350 | +------+------+------------+--------------------+ 7 rows in set (0.00 sec)
Now, MySQL SUM() function returns 0 when we use COALESCE function with SUM() to find the total number of pages typed by ‘Mohan’, the name which is not in the ‘Name’ column −
mysql> SELECT COALESCE(SUM(daily_typing_pages),0)AS ‘SUM(daily_typing_pages)’FROM employee_tbl WHERE Name = ‘Mohan’; +-------------------------+ | SUM(daily_typing_pages) | +-------------------------+ | 0 | +-------------------------+ 1 row in set (0.00 sec)
- Related Articles
- How can I customize value, instead of NULL, of a row by using MySQL IF() function?
- How can MySQL COALESCE() function be used with MySQL SUM() function to customize the output?
- When MySQL LOCATE() function returns NULL as the output?
- When MySQL FIND_IN_SET() function returns NULL as output?
- How MySQL SUM() function evaluates if it is used with SELECT statement that returns no matching rows?
- How can I return 0 for NULL in MySQL?
- Sum if all rows are not null else return null in MySQL?
- How to convert MySQL null to 0 using COALESCE() function?
- How do I get SUM function in MySQL to return '0' if no values are found?
- What MySQL MAKE_SET() function returns if there are all NULL at the place of strings?
- When MySQL IN() function returns NULL?
- When MySQL MAKE_SET() function returns NULL?
- How can I use RAND() function in an ORDER BY clause to shuffle MySQL set of rows?
- How can I set 0 if a query returns a null value in MySQL?
- How can I insert a value in a column at the place of NULL using MySQL COALESCE() function?

Advertisements