← Back to SQL, page 9

sql-0172 · Hard senior

What does ROUND(2.5) return, and does ROUND(-2.5) round away from zero on SQLite and PostgreSQL?

SQLite rounds halves away from zero, giving 3.0 and -3.0. PostgreSQL's round on numeric also rounds halves away from zero (3 and -3), but round on double precision uses the platform's rounding, which is round-half-to-even, giving 2 and -2. The type of the argument decides the rule.

Full answer
sql
SELECT ROUND(2.5) AS pos, ROUND(-2.5) AS neg, ROUND(3.5) AS pos2, typeof(ROUND(2.5)) AS t;
posnegpos2t
3.0-3.04.0real

SQLite returns a real (3.0), not an integer.

sql
SELECT ROUND(2.5) AS pos, ROUND(-2.5) AS neg, ROUND(3.5) AS pos2;
posnegpos2
3-34

PostgreSQL treats 2.5 as numeric, so halves go away from zero. Cast to double precision and the result changes:

sql
SELECT ROUND(2.5::float8) AS pos, ROUND(-2.5::float8) AS neg, ROUND(3.5::float8) AS pos2;
posnegpos2
2.0-2.04.0

Banker's rounding (half to even) avoids upward bias when rounding many values, which matters in finance and statistics. When rounding rules matter, use exact types, apply the rule explicitly, and test the halfway cases.

sql
SELECT ROUND(2.5) AS pos, ROUND(-2.5) AS neg, ROUND(3.5) AS pos2, typeof(ROUND(2.5)) AS t;

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • SQLite rounds halves away from zero and returns a real
  • PostgreSQL numeric rounds halves away from zero
  • PostgreSQL double precision rounds half to even
  • The argument type decides the rounding rule

Common mistakes

  • Assuming every engine and type rounds 2.5 to 3
  • Rounding money stored as floats

Follow-ups

  • What does ROUND(1.005, 2) return with floats?
  • When is banker's rounding preferred?

Tips

  • Test halfway values explicitly