Integer vs Decimal Division
Avoid surprising results with casting.
A Surprising Result
Let's start with a puzzle. What do you think the following query returns?
If you expect 0.5, you may be surprised. In many SQL databases, dividing two integers gives an integer result — dropping the decimal part entirely.
SELECT 1 / 2 AS result;Integer Division Explained
When both operands in a division are integers, SQL performs integer division. The result is truncated toward zero — no rounding, just the whole number part.
So 1 / 2 = 0, 7 / 3 = 2, and 9 / 4 = 2. The fractional part is silently discarded.
SELECT 7 / 3 AS truncated_result;All lessons in this course
- Arithmetic and Operators
- Rounding and Truncating
- Integer vs Decimal Division
- Useful Math Functions