Division by Zero
Error message
division by zero
Quick summary
Your query contains a division operation where the denominator is zero, which is mathematically undefined.
Why this happens
A column used as a denominator contains a zero value
A computed denominator evaluates to zero
Aggregating data where a count or sum used as a divisor is zero
Minimal example
SELECT total / count FROM stats;
-- If count is 0 for any row → division by zeroSELECT total / NULLIF(count, 0) FROM stats;
-- Returns NULL instead of erroring when count is 0NULLIF(count, 0) returns NULL when count is 0, so the division yields NULL instead of raising an error.
How to diagnose
Find the division operation in your query
Check if the denominator column or expression can be zero
Run a query to find rows where the denominator is zero
How to fix
Use NULLIF to convert zero to NULL:
numerator / NULLIF(denominator, 0)Add a WHERE clause to exclude rows where the denominator is zero
Use a CASE expression to handle the zero case specially
How to prevent
Always use NULLIF for divisions where the denominator might be zero
Validate data to prevent zero denominators
Test queries with edge cases including zero values
Related Resources
Related Glossary
Related Lessons
- Select Queries
Learn about SQL expressions and edge cases.