Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Reviewing SQL and numbers
So you think you can count
8. Integer division
Round and other functions
Review and practice

Instruction

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 1/4 is 0.25).

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 1/4.

Exercise

Run another example query to see what happens now.