COALESCE Function

The COALESCE function returns the first non-NULL value from a list of arguments. It is useful for handling NULL values in your data.

Example: Using COALESCE

SELECT Name, COALESCE(City, 'Unknown City') AS City
FROM FreedomFighters;

Output:

Returns the name of the freedom fighters and the city, replacing NULL values with 'Unknown City'.

Do's and Don'ts

Do's

  • Use COALESCE to handle NULL values effectively.
  • Provide meaningful default values when possible.

Don'ts

  • Don't use COALESCE with values that should remain NULL for data integrity.
  • Don't list too many arguments without necessity; keep it simple.