
- 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 do I get SUM function in MySQL to return '0' if no values are found?
To return Sum as ‘0’ if no values are found, use IFNULL or COALESCE commands.
The following is the syntax for IFNULL.
SELECT IFNULL(SUM(NULL), 0) AS aliasName;
Let us now implement the above syntax in the following query.
mysql> SELECT IFNULL(SUM(NULL), 0) AS SUMOFTWO;
The following is the output of the above query, which returns 0.
+----------+ | SUMOFTWO | +----------+ | 0 | +----------+ 1 row in set (0.00 sec)
Here is the syntax for COALESCE.
mysql> SELECT COALESCE(SUM(NULL),0) as SUMOFTWO;
The following is the output that returns 0 using the SUM() function.
+----------+ | SUMOFTWO | +----------+ | 0 | +----------+ 1 row in set (0.00 sec)
- Related Articles
- How to select sum or 0 if no records exist in MySQL?
- How can I customize the output of MySQL SUM() function to 0 instead of NULL when there are no matching rows?
- How can I return 0 for NULL in MySQL?
- What will MySQL CHAR_LENGTH() function return if I provide NULL to it?
- How MySQL SUM() function evaluates if the column having NULL values too?
- MySQL SUM function to add decimal values
- How to return only unique values (no duplicates) in MongoDB?
- How MySQL evaluates if I will use an expression within SUM() function?
- Sum if all rows are not null else return null in MySQL?
- What would be the output of MySQL SUM() function if a column having no values has been passed as its argument?
- How MySQL SUM() function evaluates if it is used with SELECT statement that returns no matching rows?
- How do I return multiple results in a MySQL subquery with IN()?
- How do I get the average string length in MySQL?
- How do I update NULL values in a field in MySQL?
- MySQL query to return the count of only NO values from corresponding column value

Advertisements