SQL
Provide Default Values for NULL Data with COALESCE
Replace NULL values in your query results with meaningful default values, improving data readability and consistency using the COALESCE function.
SELECT
product_id,
product_name,
COALESCE(description, 'No description available') AS product_description,
COALESCE(price, 0.00) AS product_price
FROM
products;
How it works: This snippet demonstrates how to handle `NULL` values gracefully using the `COALESCE` function. `COALESCE` returns the first non-null expression in its list. Here, if `description` is `NULL`, it will display 'No description available'. Similarly, if `price` is `NULL`, it will default to `0.00`, ensuring no `NULL` values appear in the final output for these columns.