sqlcmd.net validated sql reference
beginner math MySQL MariaDB SQL Server PostgreSQL SQLite

Round Numbers With ROUND, CEIL, and FLOOR

Control the precision or snapping of numeric results using `ROUND` for half-up rounding, `CEIL` to round up to the next integer, and `FLOOR` to round down.

Docker-validated Not currently validation-green

Round, floor, and ceil the unit cost of each order

100.00 / 3 = 33.333...: ROUND to 2 places gives 33.33, FLOOR discards the fraction to give 33, CEIL bumps up to 34. 50.00 / 4 = 12.5: ROUND to 2 places gives 12.50 (already at 2 decimal places), FLOOR gives 12, CEIL gives 13. 75.00 / 10 = 7.5: same logic as 12.5.

MySQL MariaDB PostgreSQL
Engine-specific syntax
Setup
CREATE TABLE orders (id INT, total DECIMAL(10, 2), qty INT);

INSERT INTO
  orders
VALUES
  (1, 100.00, 3),
  (2, 50.00, 4),
  (3, 75.00, 10);
SQL
SELECT
  id,
  ROUND(total / qty, 2) AS unit_cost,
  FLOOR(total / qty) AS floor_cost,
  CEIL(total / qty) AS ceil_cost
FROM
  orders
ORDER BY
  id;
idunit_costfloor_costceil_cost
133.333334
212.501213
37.5078
SQL Server
Engine-specific syntax
Setup
CREATE TABLE orders (id INT, total DECIMAL(10, 2), qty INT);

INSERT INTO
  orders
VALUES
  (1, 100.00, 3),
  (2, 50.00, 4),
  (3, 75.00, 10);
SQL
SELECT
  id,
  ROUND(total / qty, 2) AS unit_cost,
  FLOOR(total / qty) AS floor_cost,
  CEILING(total / qty) AS ceil_cost
FROM
  orders
ORDER BY
  id;
idunit_costfloor_costceil_cost
133.3333.0034.00
212.5012.0013.00
37.507.008.00
SQLite
Engine-specific syntax
Setup
CREATE TABLE orders (id INT, total DECIMAL(10, 2), qty INT);

INSERT INTO
  orders
VALUES
  (1, 100.00, 3),
  (2, 50.00, 4),
  (3, 75.00, 10);
SQL
SELECT
  id,
  ROUND(total / qty, 2) AS unit_cost,
  FLOOR(total / qty) AS floor_cost,
  CEIL(total / qty) AS ceil_cost
FROM
  orders
ORDER BY
  id;
idunit_costfloor_costceil_cost
133.333334
212.51213
37.578

SQL Server uses CEILING and preserves the DECIMAL scale on FLOOR/CEILING results. SQLite returns real values. Numeric values are identical across all engines.

Where this command helps.

  • rounding monetary values to two decimal places before display or storage
  • binning a continuous measurement into integer or 10-unit buckets

What the command is doing.

ROUND(n, d) rounds value n to d decimal places using round-half-away-from-zero. A negative d rounds to the left of the decimal — ROUND(1234, -2) returns 1200. FLOOR(n) returns the largest integer not greater than n; CEIL(n) (or CEILING(n) in SQL Server) returns the smallest integer not less than n. All three functions accept negative inputs and are supported across every major engine.