A division operation receives zero as the denominator in a query.
SQL databases like PostgreSQL throw `ERROR: division by zero` when a mathematical expression attempts to divide by 0.
Dividing aggregates or column metrics by a count or total that evaluates to zero for certain rows or groups.
1SELECT product_id, total_revenue / total_units AS avg_unit_price2FROM sales_summary; -- Fails if total_units is 0 for any product!1-- Fix 1: Use NULLIF (returns NULL when equal to 0, avoiding division error)2SELECT product_id, total_revenue / NULLIF(total_units, 0) AS avg_unit_price3FROM sales_summary;4 5-- Fix 2: CASE statement6SELECT product_id,7 CASE WHEN total_units = 0 THEN 08 ELSE total_revenue / total_units9 END AS avg_unit_price10FROM sales_summary;Simulate standard system builds to trigger compiler trace records and track memory crashes locally.
If `total_units` is `0` for any row in `sales_summary`, the query aborts with a division by zero error.