KITFORMA · EDITORIAL ANSWER
Why does SUM return NULL for an empty result instead of zero?
KitForma editorial guide. In PostgreSQL, most aggregates other than count return NULL when no input rows contribute. An unknown total and a known zero are not automatically equivalent.
Step-by-step answer
COALESCE(SUM(amount), 0) when the application contract defines an empty total as zero. Test no rows, all-null amounts and an actual zero amount separately. Do not apply COALESCE before understanding missing data: replacing unknown measurements with zero can bias averages. Choose a fallback type compatible with the aggregate, especially for numeric currency or interval results.Sources and verification
Sources checked:
Scope: This editorial guide is based on the cited sources and tool behavior. A forum question or a query observed for our site does not establish market search volume, low competition, guaranteed rankings or inadequate answers elsewhere.
This editorial answer was prepared by KitForma with AI assistance. It is not presented as a real member question or an independent user review. Check the sources and the result with your own file; report corrections in the discussion.