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.

Answer from eggyal on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ sql โ€บ func_mysql_coalesce.asp
MySQL COALESCE() Function
โฎ Previous โฎ MySQL Functions Next โฏ ยท 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.
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    |
------------------------------
๐ŸŒ
MySQL
dev.mysql.com โ€บ doc โ€บ en โ€บ comparison-operators.html
MySQL :: MySQL 8.4 Reference Manual :: 14.4.2 Comparison Functions and Operators
The return type of COALESCE() is the aggregated type of the argument types. mysql> SELECT COALESCE(NULL,1); -> 1 mysql> SELECT COALESCE(NULL,NULL,NULL); -> NULL ยท EXISTS(query) Whether the result of a query contains any rows.
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ mysql โ€บ mysql-coalesce
MySQL COALESCE Expression: Usage & Examples
The `COALESCE` expression in MySQL returns the first non-null value from a list of expressions.
๐ŸŒ
Database Guide
database.guide โ€บ mysql-coalesce-explained
MySQL COALESCE() Explained
In MySQL, the COALESCE() operator returns the first non-NULL value in the list, or NULL if there are no non-NULL values.
๐ŸŒ
Medium
suparnachowdhury.medium.com โ€บ mysql-cleaning-and-transformation-commands-cast-coalesce-case-and-more-71a3dddc27a6
MySQL Cleaning and Transformation Commands: CAST, COALESCE, CASE and More | by Suparna Chowdhury | Medium
April 25, 2025 - In this query, the COALESCE function checks the phone_number column first, then email column, and if both are NULL, it returns the message 'No contact information provided'.
๐ŸŒ
Knowledgewalls
knowledgewalls.com โ€บ johnpeter โ€บ books โ€บ mysql โ€บ coalesce-method-in-mysql-with-example
COALESCE() method in Mysql with Example in Methods of MySQL
All type of DML, DDL queries and More functions of MySQL with example. Mysql configuration and Different between and Hot topics of MySQL and More.
Find elsewhere
๐ŸŒ
Bobcares
bobcares.com โ€บ blog โ€บ coalesce-alternative-mysql
COALESCE Alternative MySQL
August 24, 2023 - Our MySQL Support team is here to help you with your questions and concerns. Did you know that the COALESCE function in MySQL is used to return the first non-null value from a list of expressions?
๐ŸŒ
OneUptime
oneuptime.com โ€บ home โ€บ blog โ€บ how to use mysql coalesce function
How to Use MySQL COALESCE Function
3 weeks ago - Learn how to use the MySQL COALESCE function to return the first non-NULL value from a list, handling missing data cleanly in queries and reports.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ sql โ€บ mysql-coalesce-function
MySQL COALESCE() Function - GeeksforGeeks
July 12, 2025 - The COALESCE function in MySQL is used to get the first non-null value from a list of expressions.
๐ŸŒ
Educative
educative.io โ€บ blog โ€บ coalesce-function-in-sql
How to use the COALESCE() function in SQL
While the DEFAULT keyword works during record insertion, COALESCE() can be applied to dynamically replace NULL values in existing data. When creating the Employees table, you can set a default value for sales_revenue: ... The IFNULL() function in MySQL replaces NULL with a specified value.
๐ŸŒ
Hightouch
hightouch.com โ€บ sql-dictionary โ€บ sql-coalesce
SQL COALESCE - Syntax, Use Cases, and Examples | Hightouch
December 29, 2023 - The SQL COALESCE function is used to handle NULL values in a database query by returning the first non-NULL value from a list of expressions or column values. It allows you to provide a default or fallback value when NULL values are encountered ...
๐ŸŒ
LearnSQL.com
learnsql.com โ€บ blog โ€บ coalesce-function-sql
How to Use the COALESCE() Function in SQL | LearnSQL.com
May 24, 2022 - You can use the COALESCE() function with more than two arguments. Suppose we want a report that lists products and their subcategories. If the subcategory is NULL, we want to replace the subcategory with the category. And if both subcategory and category are NULL, we want to replace them with the family of the product.
๐ŸŒ
EDUCBA
educba.com โ€บ home โ€บ data science โ€บ data science tutorials โ€บ mysql tutorial โ€บ mysql coalesce()
MySQL COALESCE() | Quick Glance on MySQL COALESCE()
June 2, 2023 - MySQL COALESCE function is used to handle the NULL values in the table columns. Whenever we create a table containing columns without a default value or auto-increment attribute, the values inserted in those columns by default are NULL.
Call ย  +917738666252
Address ย  Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
Beekeeper Studio
beekeeperstudio.io โ€บ blog โ€บ mysql-coalesce
MySQL COALESCE: Handling NULL Values Effectively | Beekeeper Studio
July 24, 2025 - Learn how MySQL COALESCE handles NULLs by returning the first non-NULL value, ensuring meaningful query results.
๐ŸŒ
w3resource
w3resource.com โ€บ mysql โ€บ comparision-functions-and-operators โ€บ coalesce-function.php
MySQL COALESCE() function - w3resource - w3resource
March 3, 2026 - COALESCE(estd, country, pub_city): The COALESCE function checks each column in the order they are listed and returns the first non-null value it finds. If estd is not null, it will return estd; if estd is null, it checks country; if both estd ...
๐ŸŒ
W3Schools
w3schools.com โ€บ sql โ€บ sql_isnull.asp
W3Schools.com
COALESCE() - The preferred standard. (Works in MySQL, SQL Server and Oracle)
๐ŸŒ
Interviewmaster
interviewmaster.ai โ€บ sql skills โ€บ fundamentals โ€บ coalesce
SQL COALESCE Function | Handling NULL Values | Interview Master
COALESCE is more flexible than database-specific NULL-handling functions. Advantage: COALESCE is part of the SQL standard and works across different database systems, whereas functions like IFNULL (MySQL) and NVL (Oracle) are database-specific.
๐ŸŒ
SQL Academy
sql-academy.org โ€บ functions mysql โ€บ coalesce
COALESCE โ€” MySQL Function Reference
A comprehensive guide to SQL functions with examples for MySQL and PostgreSQL - Find out how the COALESCE function works in SQL MySQL. Returns the first non-null element of the list
๐ŸŒ
W3Schools
w3schools.com โ€บ mysql โ€บ mysql_ifnull.asp
MySQL IFNULL() and COALESCE() Functions
The COALESCE() function returns the first non-NULL value in a list of values.