
- 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 use Coalesce in MySQL?
The coalesce in MySQL can be used to return first not null value. If there are multiple columns, and all columns have NULL value then it returns NULL otherwise it will return first not null value.
The syntax is as follows.
SELECT COALESCE(yourColumnName1,yourColumnName2,yourColumnName3,.......N) as anyVariableName from yourTableName;
To understand the above syntax, let us first create a table. The query to create a table is as follows.
mysql> create table CoalesceDemo -> ( -> Id int, -> Name varchar(100), -> Age int, -> Address varchar(100) -> ); Query OK, 0 rows affected (0.57 sec)
Insert some records in the table using insert command. The query is as follows.
mysql> insert into CoalesceDemo values(1,NULL,NULL,NULL); Query OK, 1 row affected (0.14 sec) mysql> insert into CoalesceDemo values(NULL,'Mike',NULL,NULL); Query OK, 1 row affected (0.17 sec) mysql> insert into CoalesceDemo values(NULL,NULL,23,NULL); Query OK, 1 row affected (0.24 sec) mysql> insert into CoalesceDemo values(NULL,NULL,NULL,'UK'); Query OK, 1 row affected (0.14 sec) mysql> insert into CoalesceDemo values(NULL,NULL,NULL,NULL); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement. The query is as follows.
mysql> select *from CoalesceDemo;
The following is the output.
+------+------+------+---------+ | Id | Name | Age | Address | +------+------+------+---------+ | 1 | NULL | NULL | NULL | | NULL | Mike | NULL | NULL | | NULL | NULL | 23 | NULL | | NULL | NULL | NULL | UK | | NULL | NULL | NULL | NULL | +------+------+------+---------+ 5 rows in set (0.00 sec)
Here are all the cases where you can return the first NOT NULL value. The query is as follows.
mysql> select Coalesce(Id,Name,Age,Address) as FirstNotNullValue from CoalesceDemo;
The following is the output.
+-------------------+ | FirstNotNullValue | +-------------------+ | 1 | | Mike | | 23 | | UK | | NULL | +-------------------+ 5 rows in set (0.00 sec)
- Related Articles
- How can I use IFNULL() function at the place of COALESCE() function in MySQL?
- How to convert MySQL null to 0 using COALESCE() function?
- How can MySQL COALESCE() function be used with MySQL SUM() function to customize the output?
- Display first non-null values with coalesce() in MySQL?
- How can we apply COALESCE() function on a MySQL table’s data value?
- How to use MySQL decimal?
- What MySQL COALESCE() function returns if all the arguments provided to it are NULL?
- How to use Straight Join in MySQL?
- How to use TIME type in MySQL?
- How to use SELF JOIN in MySQL?
- How to use % wildcard correctly in MySQL?
- How to use GROUP_CONCAT in CONCAT in MySQL?
- How can I insert a value in a column at the place of NULL using MySQL COALESCE() function?
- How to use compound INTERVAL unit in MySQL?
- How to use NULL in MySQL SELECT statement?

Advertisements