Learn SQL
Handling NULL
30.3
Lesson

COALESCE

Let’s say we want to display the name of all students with one additional data point. The additional data point is the first of age, grade and course which is not NULL. So, if age is available we print it, but if it’s not we print the grade value. If the grid value is also NULL, we print the course value. If the course value is NULL as well, we return NULL.

This requires selecting first of the not-null values from a list. SQL provides a COALESCE function to achieve this.

Observe that COALESCE takes a list of values as arguments, and returns the first not-null value from it as a result.