🌐
W3Schools
w3schools.com › sql › func_mysql_coalesce.asp
MySQL COALESCE() Function
The COALESCE() function returns the first non-null value in a list. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
🌐
W3Schools
w3schools.com › mysql › mysql_ifnull.asp
MySQL IFNULL() and COALESCE() Functions
... The "InOrder" column is optional, and may contain NULL values. ... Note: In the SQL above, if any of the "InOrder" values are NULL, the result will be NULL! The COALESCE() function is the preferred standard for handling potential NULL values.
🌐
w3resource
w3resource.com › mysql › comparision-functions-and-operators › coalesce-function.php
MySQL COALESCE() function - w3resource - w3resource
March 3, 2026 - In dynamic SQL, where values might be unknown or changeable, COALESCE() is useful for providing a fallback value. It aids in data transformation by providing a way to replace NULL values with meaningful alternatives, making it easier to work ...
🌐
Cach3
w3schools.com.cach3.com › sql › func_mysql_coalesce.asp.html
MySQL COALESCE() Function - W3Schools
❮ MySQL Functions · Return the first non-null value in a list: SELECT COALESCE(NULL, NULL, NULL, 'W3Schools.com', NULL, 'Example.com'); Try it Yourself » · The COALESCE() function returns the first non-null value in a list. COALESCE(val1, val2, ...., val_n) Return the first non-null value ...
🌐
W3Schools
w3schools.com › sql › sql_isnull.asp
SQL COALESCE(), IFNULL(), ISNULL(), NVL() Functions
COALESCE() - The preferred standard. (Works in MySQL, SQL Server and Oracle)
🌐
W3schools-fa
w3schools-fa.ir › sql › func_mysql_coalesce.html
MySQL COALESCE() Function
❮ MySQL Functions · Return the first non-null value in a list: SELECT COALESCE(NULL, NULL, NULL, 'W3Schools.com', NULL, 'Example.com'); Try it Yourself » · The COALESCE() function returns the first non-null value in a list. COALESCE(val1, val2, ...., val_n) Return the first non-null value ...
🌐
MySQL Tutorial
mysqltutorial.org › home › mysql comparison functions › mysql coalesce function
MySQL COALESCE Function
October 10, 2023 - Summary: In this tutorial, you will learn how to use the MySQL COALESCE function to substitute NULL values.
Top answer
1 of 5
48
  1. How come I saw queries that returns multiple values? isnt it only the first not null value that is returned?

    Yes, it is only the first non-NULL value that is returned. You must be mistaken about the queries you have seen where you thought that was not the case: if you could show us an example, we might be able to help clarify the misunderstanding.

  2. And how do it decide which column to base? coalesce(column1,column2)? what if first column is null and other column is not null?

    In order of its arguments: in this example, column1 before column2.

  3. Or if im wrong or my syntax is wrong, how do i properly write it?

    You're not wrong.

  4. Can someone provide a very good and simple example on how to use it?

    Taken from the documentation:

    mysql> SELECT COALESCE(NULL,1);
            -> 1
    mysql> SELECT COALESCE(NULL,NULL,NULL);
            -> NULL
    ``
    
  5. And when it is desirable to use.

    It is desirable to use whenever one wishes to select the first non-NULL value from a list.

2 of 5
44

I personally use coalesce when I want to find the first column that isn't blank in a row from a priority list.

Say for example I want to get a phone number from a customer table and they have 3 columns for phone numbers named mobile, home and work, but I only want to retrieve the first number that isn't blank.

In this instance, I have the priority of mobile, then home and then work.

TABLE STRUCTURE
--------------------------------------------
| id | customername | mobile | home | work |
--------------------------------------------
| 1  | Joe          | 123    | 456  | 789  |
--------------------------------------------
| 2  | Jane         |        | 654  | 987  |
--------------------------------------------
| 3  | John         |        |      | 321  |
--------------------------------------------

SELECT id, customername, COALESCE(mobile, home, work) AS phone FROM customers

RESULT
------------------------------
| id | customername | phone  |
------------------------------
| 1  | Joe          | 123    |
------------------------------
| 2  | Jane         | 654    |
------------------------------
| 3  | John         | 321    |
------------------------------
🌐
TechOnTheNet
techonthenet.com › mysql › functions › coalesce.php
MySQL: COALESCE Function
This MySQL tutorial explains how to use the MySQL COALESCE function with syntax and examples. The MySQL COALESCE function returns the first non-null expression in the list.
Find elsewhere
🌐
W3Schools
w3schools.com › MySQL › mysql_syllabus.asp
MySQL Syllabus (Curriculum)
String Functions ASCII CHAR_LENGTH ... CAST COALESCE CONNECTION_ID CONV CONVERT CURRENT_USER DATABASE IF IFNULL ISNULL LAST_INSERT_ID NULLIF SESSION_USER SYSTEM_USER USER VERSION · MySQL Examples MySQL Editor MySQL Quiz MySQL Exercises MySQL Syllabus MySQL Study Plan MySQL Certificate ... The W3Schools MySQL Tutorial ...
🌐
DataCamp
datacamp.com › doc › mysql › mysql-coalesce
MySQL COALESCE Expression: Usage & Examples
Learn how to use the MySQL COALESCE expression to handle null values effectively, ensuring your queries return meaningful results with practical examples and best practices.
🌐
GeeksforGeeks
geeksforgeeks.org › sql › mysql-coalesce-function
MySQL COALESCE() Function - GeeksforGeeks
July 12, 2025 - In this example, we will use the COALESCE function on a list of values where the first value is not NULL ... MySQL COALESCE() function is used to return the first non-NULL value from a list of expressions.
🌐
W3Schools
w3schools.com › MYSQL › default.asp
MySQL Tutorial
String Functions ASCII CHAR_LENGTH ... Advanced Functions BIN BINARY CASE CAST COALESCE CONNECTION_ID CONV CONVERT CURRENT_USER DATABASE IF IFNULL ISNULL LAST_INSERT_ID NULLIF SESSION_USER SYSTEM_USER USER VERSION · MySQL Examples MySQL Editor MySQL Quiz MySQL Exercises ...
🌐
W3Schools
w3schools.com › mysql › mysql_sql.asp
MySQL SQL
String Functions ASCII CHAR_LENGTH ... Advanced Functions BIN BINARY CASE CAST COALESCE CONNECTION_ID CONV CONVERT CURRENT_USER DATABASE IF IFNULL ISNULL LAST_INSERT_ID NULLIF SESSION_USER SYSTEM_USER USER VERSION · MySQL Examples MySQL Editor MySQL Quiz MySQL Exercises ...
🌐
W3Schools
w3schools.com › mysql › trymysql.asp
MySQL Tryit Editor v1.0
Edit the SQL Statement, and click "Run SQL" to see the result · This SQL-Statement is not supported in the WebSQL Database
🌐
CastorDoc
castordoc.com › how-to › how-to-use-coalesce-in-mysql
How to use coalesce in MySQL?
Learn how to effectively use the coalesce function in MySQL to handle null values and streamline your database queries.
🌐
W3Schools
w3schools.com › mysql › mysql_select.asp
MySQL SELECT Statement
String Functions ASCII CHAR_LENGTH ... Advanced Functions BIN BINARY CASE CAST COALESCE CONNECTION_ID CONV CONVERT CURRENT_USER DATABASE IF IFNULL ISNULL LAST_INSERT_ID NULLIF SESSION_USER SYSTEM_USER USER VERSION · MySQL Examples MySQL Editor MySQL Quiz MySQL Exercises ...
🌐
TutorialsPoint
tutorialspoint.com › mysql › mysql-coalesce-function.htm
MySQL - COALESCE() Function
The MySQL COALESCE() function returns the first non-NULL value in a list of expressions. It takes multiple expressions as arguments and returns the value of the first expression that is not NULL.
🌐
W3Schools
w3schools.com › mysql › mysql_intro.asp
MySQL Introduction
String Functions ASCII CHAR_LENGTH ... Advanced Functions BIN BINARY CASE CAST COALESCE CONNECTION_ID CONV CONVERT CURRENT_USER DATABASE IF IFNULL ISNULL LAST_INSERT_ID NULLIF SESSION_USER SYSTEM_USER USER VERSION · MySQL Examples MySQL Editor MySQL Quiz MySQL Exercises ...