Wait… what? We got a
0? After all those years we've been taught it's
0.25? Has someone deceived us?
Not really, no. What happened here is called integer division. Integer division in SQL takes place when both the dividend and the divisor are integers. Since they are integers, SQL wants to return an integer result to match the number type. In other words, it brutally cuts off the decimal part, which is
.25 in our case. Zero (
0) is the only thing left.
What is more, the meaning of the operator
/ differs among databases. In PostgreSQL and SQL Server, it is integer division. In MySQL and Oracle it is regular division (and the result of
So, how can we make sure that the result includes the decimal part?
One thing you can do is change the type of one of the values to decimal: write
1/4.0 instead of