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:
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.
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.
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:
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.
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: