×

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 RIGHT() Function

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

3 minutes read.

MySQL HEX() function

In this context, we will learn how we can use the MySQL HEX() function with proper syntax and good examples. Introduction of MySQL HEX() function For returning an equivalent hexadecimal string value...

3 minutes read.

MySQL INNER JOIN

MySQL inner join connects two tables by using their common columns. It is the basic join of the MySQL system. It is used to returns only those results from the...

4 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 LOCATE() function

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

3 minutes read.

MySQL PI() function

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

2 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 ROUND() function

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

3 minutes read.

MySQL Triggers

MySQL trigger is a function of the stored procedure to respond to the system program. This function responds and runs any data table event automatically. You can use it for...

5 minutes read.

MySQL LOG() function

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

3 minutes read.

MySQL DAY() function

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

2 minutes read.

MySQL CROSS JOIN

MySQL CROSS JOIN combines all possibilities of the two or more tables and returns the result that contains every row from all contributing tables. It links several columns from the...

4 minutes read.

MySQL Command line client Basic Queries

MySQL – command line client Basic Queries There are many queries to work with the MySQL command-line client. Today, we will explore MySQL basic queries and their function. The MySQL query...

8 minutes read.

MySQL REVERSE() function

In this context, we will learn how we can use the MySQL REVERSE() function with proper syntax and good examples. Introduction of MySQL REVERSE() function This function could be used to reverse...

2 minutes read.

MySQL Clauses

MySQL Clauses MySQL system clauses are keywords or statements to handle information. It helps to operate a group of the data and apply it to require conditions. The clauses apply conditions...

16 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 QUARTER() function

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

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 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 Prefix index

Prefix index This index query creates an index column in the string or character column. MySQL system can create multiple indexes in the table. It will create in a table as...

3 minutes read.