Search code examples
mysqlsqldbt

What does a zero value coalesce function do?


I have been exploring dbt tools and I came across the following code snippet :

coalesce(customer_orders.number_of_orders, 0) as number_of_orders

I understand that a coalesce function is used to return the first non-null value in a list. What I do not understand is what does the zero in the second parameter signify?


Solution

  • The COALESCE function returns the first non-null value in a list. COALESCE can take n number of arguments.

    COALESCE(val1, val2, ...., val_n)
    

    So according to the query:

    coalesce(customer_orders.number_of_orders, 0) as number_of_orders
    

    In case customer_orders.number_of_orders is NULL the result returned in number_of_orders would be 0.