COALESCE( )

This MySQL function returns the leftmost non-NULL string or column in a comma-separated list.

Syntax

Below is the syntax for this command. Text within square brackets (i.e, [ and ] ) are optional. Choices are separated by bars (i.e, | ). Ellipses preceded by a comma indicates a repeating pattern. Ellipses before or after syntax are used to highlight a relevant except from the larger syntax. Text displayed in italic letters represent text that should be replaced with the specific names related to the database (e.g., column with the name of the column).

COALESCE(column[,  . . . ])

Explanation

This function returns the leftmost non-NULL string or column in a comma-separated list. If all elements are NULL, the function returns NULL.

Examples

SELECT CONCAT(name_first, ' ', name_last)
  AS Student,
COALESCE(phone_dorm, phone_home, 'No Telephone Number')
  AS Telephone
FROM students;

In this example, the results will show the student's dormitory telephone number if there is one (i.e., if the student lives in the dormitory). If not, it will show the student's home telephone number (i.e., maybe his parent's house). Otherwise, it will return the string given, indicating that there is no telephone number for the student.