COALESCE()

You can use the coalesce()function as shorthand for case statements. The coalesce() function replaces null values in your dataset with another value. The function takes a series of arguments and returns the first value that is not null.

In this query, the first case statement says that if the City value is not null, return the City value. Otherwise, return NULL. For the second case, it says if a Country value is not null, return the Country. If it is null, return the string “Unknown.”

Here’s the same query rewritten with coalesce().

There are no null City or Country values in the first five results returned from the query.

CityCountry
AberdeenUnited States
AbileneUnited States
AkronUnited States
AlbuquerqueUnited States
AlexandriaUnited States