SQL error · GROUP BY and calculations

Division drops the decimals

Problem: On some engines, dividing two integers gives an integer.

Solution: Convert one operand to a decimal before dividing, for example 1.0 * 5 / 2.

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.

Minimal example
SELECT 5 / 2 AS result;
Result verified in SQLite
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.

What the problem looks like

No error appears: 5 / 2 returns 2 instead of 2.5 in PostgreSQL, SQL Server and SQLite; MySQL returns 2.5000 with the / operator.

Since the engine gives no warning, work out a small case by hand, such as 5 / 2, and compare it with the result: if the decimals are missing, the division was an integer division.

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:

Promoting the operation to decimal
SELECT 1.0 * 5 / 2 AS result;
Result verified in SQLite
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

EngineWhat happens with integersHow to keep the decimals
PostgreSQL 18Dividing integer types truncates towards zero: 5 / 2 returns 2.Use an operand numeric, for example 5.0 / 2 or a CAST.
SQL ServerIf 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.
SQLiteUses 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.