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
| PostgreSQL | SQLite | |
|---|---|---|
| Available | Yes (with digits only for numeric) | Yes |
| Syntax | ROUND(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
| species | avg_age |
|---|---|
| bird | 4 |
| cat | 4.9 |
| dog | 5.1 |
| rabbit | 2 |
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
| device | plays | pct |
|---|---|---|
| mobile | 1449 | 60.2 |
| desktop | 733 | 30.5 |
| tv | 225 | 9.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
| name | with_tax |
|---|---|
| Pepperoni Rocket | 102.06 |
| Four Cheese Orbit | 75.6 |
| BBQ Meteor | 66.96 |
| Margherita | 59.4 |
| Veggie Nebula | 42.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.
| Case | PostgreSQL | SQLite |
|---|---|---|
| Two decimals | ROUND(13.456, 2)13.46 | ROUND(13.456, 2)13.46 |
| A half | ROUND(2.5)3 | ROUND(2.5)3 |
| A negative half | ROUND(-2.5)-3 | ROUND(-2.5)-3 |
| A tricky decimal | ROUND(2.675, 2)2.68 | ROUND(2.675, 2)2.67 |
| Negative digits | ROUND(1234.5, -2)1200 | ROUND(1234.5, -2)1235 |
| Integer division first | ROUND(7 / 2)3 | ROUND(7 / 2)3 |
| A double precision value with digits | ROUND(CAST(2.5 AS DOUBLE PRECISION), 1)error: function round(double precision, integer) does not exist | ROUND(CAST(2.5 AS REAL), 1)2.5 |
| NULL | ROUND(NULL, 1)NULL | ROUND(NULL, 1)NULL |
Related
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
- Guide: SQL GROUP BY and HAVING
- Practice: SQL GROUP BY and HAVING practice: 8 exercises with answers
- Lesson: Count, sum, average (Pro)
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.