sqlstreak

SQL function

ROUND() in SQL (PostgreSQL and SQLite)

Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)

Short answer

ROUND(x, n) rounds x to n decimal places (to a whole number without n): ROUND(13.456, 2) is 13.46. Both databases round halves away from zero for ordinary values; they differ on negative n and on which types PostgreSQL accepts.

Syntax and availability

PostgreSQLSQLite
AvailableYes (with digits only for numeric)Yes
SyntaxROUND(numeric [, digits]) / ROUND(double precision)ROUND(x [, digits])

Examples

Average age, one decimal

SELECT species, ROUND(AVG(age), 1) AS avg_age
FROM pets
GROUP BY species
ORDER BY species;

Result (PostgreSQL and SQLite, identical): 4 rows

speciesavg_age
bird4
cat4.9
dog5.1
rabbit2

Try it: PostgreSQL playground · SQLite playground

A percentage

SELECT device, COUNT(*) AS plays,
       ROUND(100.0 * COUNT(*) / (SELECT COUNT(*) FROM plays), 1) AS pct
FROM plays
GROUP BY device
ORDER BY plays DESC;

Result (PostgreSQL and SQLite, identical): 3 rows

deviceplayspct
mobile144960.2
desktop73330.5
tv2259.3

Try it: PostgreSQL playground · SQLite playground

Money to two places

SELECT m.name, ROUND(SUM(oi.quantity * m.price * 1.08), 2) AS with_tax
FROM order_items oi
JOIN menu_items m ON m.id = oi.item_id
GROUP BY m.name
ORDER BY with_tax DESC
LIMIT 5;

Result (PostgreSQL and SQLite, identical): 5 rows

namewith_tax
Pepperoni Rocket102.06
Four Cheese Orbit75.6
BBQ Meteor66.96
Margherita59.4
Veggie Nebula42.12

Try it: PostgreSQL playground · SQLite playground

How it behaves: edge cases, measured

Each expression was run as SELECT … on PostgreSQL 18.6 and SQLite 3.54.1 when this page was built. Nothing here is typed by hand.

CasePostgreSQLSQLite
Two decimalsROUND(13.456, 2)13.46ROUND(13.456, 2)13.46
A halfROUND(2.5)3ROUND(2.5)3
A negative halfROUND(-2.5)-3ROUND(-2.5)-3
A tricky decimalROUND(2.675, 2)2.68ROUND(2.675, 2)2.67
Negative digitsROUND(1234.5, -2)1200ROUND(1234.5, -2)1235
Integer division firstROUND(7 / 2)3ROUND(7 / 2)3
A double precision value with digitsROUND(CAST(2.5 AS DOUBLE PRECISION), 1)error: function round(double precision, integer) does not existROUND(CAST(2.5 AS REAL), 1)2.5
NULLROUND(NULL, 1)NULLROUND(NULL, 1)NULL

Common mistakes

  • ROUND(x::float, 2) in PostgreSQL: there's no ROUND(double precision, integer). Cast to numeric instead (see the matrix and the error page).

  • Rounding an integer division: ROUND(7 / 2) is 3 because the division already threw the half away. Write 7.0 / 2.

  • Using a negative number of digits in SQLite to round to hundreds: SQLite treats it as 0 (see the matrix).

PostgreSQL and SQLite

The everyday cases match. PostgreSQL rounds numeric values exactly and accepts negative digits; SQLite works in floating point, so 2.675 (stored as a hair under it) rounds to 2.67, and it treats negative digits as 0. PostgreSQL refuses ROUND(double precision, digits).

Official documentation

Go deeper

Make it stick in five minutes a day

“Count, sum, average” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.

Start free