Using COALESCE and NULLIF
(from one comment on postgresql.org): “I never found a good use for NULLIF, until recently. Now I use NULLIF to guard against divizion by zero. This is what my typical aggregate query looks like: SELECT COALESCE(SUM(cost), 0) / NULLIF(SUM(amount), 0) as average_price FROM sales”