×

MySQL COALESCE function

This statement displays the first non-null value of the table data. The COALESCE function returns a null value when all values of the table are null or do not find any non-null value.

Syntax

The COALESCE function syntax shows below.

COALESCE (data1, data2, data3, dataN);

The COALESCE function syntax with table shows below.

SELECT COALESCE (column_name, expression) FROM table name;

The COALESCE function with WHERE clause syntax shows below.

SELECT * FROM table name WHERE COALESCE (column_name);

Prerequisite

  • Create table in the database with data type and key.
mysql> CREATE TABLE `marks` (
  `roll_no` INT NOT NULL,
  `name` VARCHAR(45) NULL,
  `physics` INT NULL,
  `chemistry` INT NULL,
  `maths` INT NULL,
  `english` INT NULL,
  PRIMARY KEY (`roll_no`));
  • Insert value in the table as per requirement.
  • The below command shows the table format and its information.
mysql> Select * from marks;

Use of the COALESCE function

The following are the uses of coalesce function:

  • The COALESCE function is used to compare the column data between null and not null values.
  • This function is used to find the not null values in the table columns.
  • This COALESCE function determines and maintains table data according to null and not null values.

Example of the COALESCE function

Example1: Execute the following query to know about the "COALESCE" statement.

mysql> SELECT COALESCE(NULL, 'Parameter', NULL);

Output

Executing the statement will show the below output:

MySQL COALESCE function

The image shows the output as a "parameter" value. If a single value is available, then the "COALESCE" function returns it.

Example2: The below query shows the working procedure of the "null" values and their output.

mysql> SELECT COALESCE(NULL, NULL);

Output

Executing the query will return the NULL value because it does not have any non-value.

MySQL COALESCE function

Example3: This example will explain the "COALESCE" function with the table. Here, we applied this function to the single column of the table.

mysql> select coalesce(physics, 'N/A') from marks;

Output

Executing the query will return the below output where we can see that the null value of the physics column replaces it with the "N/A" keyword.

MySQL COALESCE function

Example4: Execute the following query to know how the "COALESCE" function work with the WHERE condition. Here, this function applies to the single column of the table.

mysql> select coalesce(physics, 'N/A' ) from marks WHERE roll_no < 3;

Output

This query shows the below result:

MySQL COALESCE function

Example5: Execute the following query to know about the "COALESCE" statement used in the multiple columns of the table.

mysql> select coalesce(physics, 'N/A'), coalesce(chemistry, 10) , coalesce(maths, 'N/A')  from marks;

Output

It gives the below result where we can see that the null value of the given column replaces with a given value.

MySQL COALESCE function

Example6: This example explains the COALESCE function with the ORDER BY clause. Here we have applied this function to the multiple columns of the table. The "ORDER BY" clause applies to the descending order on the column.

mysql> select coalesce(physics, 'N/A'), coalesce(chemistry, 10) , coalesce(maths, 'N/A')  from marks WHERE roll_no < 4 ORDER BY physics DESC;

Output

Executing the query return the below result:

MySQL COALESCE function

Related Topics

MySQL Invisible Index

Invisible Index MySQL index decides an option to the availability of the index for the query optimizer. This index shows the visible and invisible options to displays the index column in...

3 minutes read.

MySQL TAN() Function

Basically, the TAN() function in MySQL is used to give back the tangent of a mentioned number.In any kind of right-angle triangle, the tangent of an angle is the length...

2 minutes read.

MySQL Data Types

Introduction of the Data types In the data file, we have several information such as names, numbers, marks, and other information. This information specifies categories such as numbers, characters, and decimal...

8 minutes read.

MySQL Features

Features of MySQL MySQL is a relational database management system (RDBMS): It is a collection of many programs and makes relations with many other programs. Easy to use MySQL database: MySQL...

2 minutes read.

MySQL RTRIM() function

In this context, we will learn how we can use the MySQL RTRIM() function with proper syntax and good examples. Introduction of MySQL RTRIM() function It is the function in MySQL that...

2 minutes read.

MySQL LENGTH() Function

In this context, we will learn how we can use the MySQL LENGTH() function with proper syntax and good examples. Introduction of MySQL LENGTH() function This function in MySQL is used to...

2 minutes read.

MySQL String Function

The string function is used to maintain and operate string values. This function modifies the string data as per function. We can do the concatenation, conversion, removal and replacing the...

4 minutes read.

MySQL Error 1046 - No Database Selected

What is MySQL? MySQL is a Relational Database Management System (RDBMS). SQL in MySQL is the abbreviation of "Structured Query Language", which Oracle developed it in 1995. It is one of...

1 minute read.

MySQL function

MySQL function is a piece of program to perform some specific task. Here we pass a parameter and then return a single value. MySQL function mainly supports string, numeric, conditional,...

9 minutes read.

MySQL DATE_FORMAT() function

In this context, we will learn how we can use the MySQL DATE_FORMAT() function with proper syntax and good examples. Introduction of MySQL DATE_FORMAT() function DATE_FORMAT() function in MySQL is used to...

3 minutes read.

MySQL LIKE() function

In this context, we will learn how we can use the MySQL LIKE() function with proper syntax and good examples. Introduction of MySQL LIKE() function The LIKE function is a logical function...

4 minutes read.

MySQL FIND_IN_SET() function

In this context, we will learn how we can use the MySQL MOD() function with proper syntax and good examples. Introduction of MySQL MOD() function MySQL provides a built-in string function called...

4 minutes read.

MySQL CEIL() function

In this context, we will learn how we can use the MySQL CEIL () function with proper syntax and good examples. Introduction of MySQL CEIL() function Input is taken by the CEIL()...

3 minutes read.

MySQL Queries

MySQL Queries MySQL supports SQL queries in the MySQL interface. These queries help to interact data with the application. MySQL uses create database, user database, create a table, truncate table, and...

13 minutes read.

MySQL LOAD_FILE() function

In this context, we will learn how we can use the MySQL LOAD_FILE() function with proper syntax and good examples. Introduction of MySQL LOAD_FILE() function The LOAD_FILE() function in MySQL reads a...

3 minutes read.

MySQL Control flow function

The control flow function uses values or operands for logical operations. These functions can works on single and multiple conditions. The control flow function is based on the Boolean expression....

3 minutes read.

MySQL LN() function

In this context, we will learn how we can use the MySQL LN() function with proper syntax and good examples. Introduction of MySQL LN() function For evaluation of the natural logarithm of...

2 minutes read.

MySQL FROM_BASE64() function

In this context, we will learn how we can use the MySQL FROM_BASE64()() function with proper syntax and good examples. Introduction of MySQL FROM_BASE64()() function Basically, the FROM_BASE64() function In MySQL decodes...

2 minutes read.

MySQL RPAD() function

In this context, we will learn how we can use the MySQL RPAD() function with proper syntax and good examples. Introduction of MySQL RPAD() function RPAD() function in MySQL is used to...

2 minutes read.

MySQL Environmental Setup

The MySQL Environmental Setup MySQL is free, open-source, and cross-platform software, which can be downloaded from its official website. MySQL management system installs on Linux, macOS, and windows. MySQL requires a framework...

6 minutes read.