Coalesce
coalesce accepts unlimited params, and always returns the first non-null value
- In this way, you can think of the final param as the default value.
Embracing null allows us to easily inline defaults
- You can chain a bunch of inputs and it returns the first non-null value
- ex. imagine having a result set that shows us how much revenue was earned each year since 2010. We should define a column
coalesce(income, 0) as incomein our select statement, which will return us a 0 if there is no income for the relevant year. This is a nicer alternative than to just omitting rows without data. - ex. Lets say you have an input parameter with a default, you can the just wrap the usages of that parameters like this: coalesce (_parameter, default_value) = whatever it tests against
- ex. Imagine we have a postgres function that accepts 2 args: a
start_dateand anend_date, and returns the amount of time that an employee worked at a company. If they currently work there still, the client passesnullfor the value of theend_date. In our function, we would have a smart failover that defaults the null value tonow(). - ex. imagine we have a column
discountthat defaults tonull. We want to use thisdiscountcolumn when determining subtotal, so we need to use coalesce to provide us with a default value of 0:
SELECT
product,
(price - COALESCE(discount,0)) AS net_price