← Back to all snippets
SQL

Handling NULL Values Gracefully with COALESCE

Use the `COALESCE` function to return the first non-NULL expression in a list, ensuring default or fallback values are displayed instead of NULLs in your SQL query results.

SELECT
    product_id,
    product_name,
    COALESCE(description, 'No description available') AS product_description,
    COALESCE(price, 0.00) AS actual_price
FROM
    Products;
How it works: The `COALESCE` function is used here to elegantly handle NULL values in query results. For the `description` column, if a product's description is NULL, `COALESCE` replaces it with the string 'No description available'. Similarly, if `price` is NULL, it defaults to 0.00. This ensures that your query results are always more user-friendly and consistent, avoiding blank or NULL entries for critical display fields.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs