
- 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 to avoid null result of “SELECT max(rank) FROM test” for an empty table?
You can use COALESCE() along with aggregate function MAX() for this.
The syntax is as follows
SELECT COALESCE(MAX(`yourColumnName`), 0) FROM yourTableName;
To understand the above syntax, let us create a table. The query to create a table is as follows
mysql> create table avoidNullDemo -> ( -> `rank` int -> ); Query OK, 0 rows affected (0.53 sec)
Insert some records in the table using insert command.
The query is as follows
mysql> insert into avoidNullDemo values(10); Query OK, 1 row affected (0.20 sec) mysql> insert into avoidNullDemo values(NULL); Query OK, 1 row affected (0.18 sec) mysql> insert into avoidNullDemo values(20); Query OK, 1 row affected (0.17 sec) mysql> insert into avoidNullDemo values(NULL); Query OK, 1 row affected (0.11 sec) mysql> insert into avoidNullDemo values(100); Query OK, 1 row affected (0.11 sec) mysql> insert into avoidNullDemo values(NULL); Query OK, 1 row affected (0.16 sec)
Display you all records from the table using select statement.
The query is as follows
mysql> select *from avoidNullDemo;
The following is the output with NULL values
+------+ | rank | +------+ | 10 | | NULL | | 20 | | NULL | | 100 | | NULL | +------+ 6 rows in set (0.00 sec)
Here is the query to avoid null result
mysql> select COALESCE(MAX(`rank`), 0) FROM avoidNullDemo;
The following is the output
+--------------------------+ | COALESCE(MAX(`rank`), 0) | +--------------------------+ | 100 | +--------------------------+ 1 row in set (0.00 sec)
Here is the case when table is empty.
Let us delete all records from the above table.
The query is as follows
mysql> truncate table avoidNullDemo; Query OK, 0 rows affected (0.92 sec)
Now the above table is empty. Let us implement the above query for empty table
mysql> select COALESCE(MAX(`rank`), 0) FROM avoidNullDemo;
The following is the output
+--------------------------+ | COALESCE(MAX(`rank`), 0) | +--------------------------+ | 0 | +--------------------------+ 1 row in set (0.00 sec)
- Related Articles
- How to select an empty result set in MySQL?
- How to test String is null or empty?
- How to SELECT min and max value from the part of a table in MySQL?
- How to avoid inserting NULL values to a table with JavaScript?
- Inserting rows in an empty table to test output of ABAP code
- Select and add result of multiplying two columns from a table in MySQL?
- C++ code to find rank of student from score table
- Checking for Null or Empty in Java.
- How MySQL handles the empty and null values for enumerations?
- Find the count of EMPTY or NULL columns in a MySQL table?
- Return only the non-empty and non-null values from a table and fill the empty and NULL values with the corresponding column values in MySQL?
- How to Conduct a Wilcoxon Signed-Rank Test in Python?
- How to assign values to an array with null/empty objects in JavaScript?
- MySQL query to get result from multiple select statements?
- How to select first and last data row from a MySQL result?

Advertisements