Why it happens: division drops the decimals
The problem shows up in percentages, hand-made averages, rates and ratios when both the numerator and the denominator are integers. The query is perfectly valid, but the type of the operation decides whether a fractional part can exist at all.
SELECT 5 / 2 AS result;| result |
|---|
| 2 |
In SQLite both literals are integers, so it uses integer arithmetic and truncates the quotient towards zero. PostgreSQL and SQL Server do the same for integer operands. It is not a problem with ROUND: the fraction is already gone before any rounding happens.
How to fix it in SQL
Make at least one operand take part as a decimal or approximate value. For a simple formula, a literal such as 1.0 makes the intent visible:
SELECT 1.0 * 5 / 2 AS result;| result |
|---|
| 2.5 |
When the final type matters (for example money, or a metric that must keep a specific scale), it is better to state the requirement with CAST to a decimal type suitable for your engine rather than relying on a literal.
Mental model: first decide which type the calculation needs; then divide; finally, round if the presentation requires it. ROUND(5 / 2, 2) does not bring back a fraction that integer division has already removed.
How to check it
The fixed query should return 2.5 for 5 divided by 2 on whichever engine you test it.
How to avoid it next time
Changing the type after dividing does not bring back a fraction that is already lost.
Related cases worth checking
The same expression does not mean exactly the same on every engine
| Engine | What happens with integers | How to keep the decimals |
|---|---|---|
| PostgreSQL 18 | Dividing integer types truncates towards zero: 5 / 2 returns 2. | Use an operand numeric, for example 5.0 / 2 or a CAST. |
| SQL Server | If the dividend and divisor are integers, the result is an integer and the fractional part is truncated. | Convert one of the operands to decimal/numeric before dividing. |
| SQLite | Uses integer arithmetic when both operands are integers and there is no overflow; integer division truncates towards zero. | Bring in a real value, for example 1.0 * numerator / denominator, or apply a suitable CAST. |
| MySQL 8.4 | / does not act as integer division: with exact values it keeps the scale. The DIV operator, however, does drop the fractional part. | For quotients with decimals, use /; avoid replacing it with DIV if you need the fraction. |
This difference matters when you move a query between engines. A formula that already returns decimals in MySQL can be truncated when you migrate it to PostgreSQL, SQL Server or SQLite if its columns are integers.
A common case: percentages that look too small
If you compute an integer ratio first and then multiply by 100, the loss can be even more obvious:
-- Risk in engines that use integer division
SELECT 2 / 3 * 100 AS percentage;
-- Use a decimal operation before dividing
SELECT 100.0 * 2 / 3 AS percentage;In SQLite, the first expression returns 0 because 2 / 3 is truncated before the multiplication. The second keeps the fraction and gives roughly 66,67 when rounded to two decimal places.