Find the Division by Zero Mistake
Find the Mistake Medium
Question
This query can fail with division by zero. Find and fix it:
Code
SELECT SUM(sales) / COUNT(sales) FROM orders;
Hint
What if COUNT(sales) is 0?
Explanation
If there are no rows (or no non-NULL sales values), COUNT(sales) is 0, causing division by zero. Fix: use NULLIF to convert zero to NULL: SELECT SUM(sales) / NULLIF(COUNT(sales), 0) FROM orders; — this returns NULL instead of erroring.
Related Resources
Related Lessons
- Aggregations
Learn safe division.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.