Stop 04 · Functions & ExpressionsTopics · 3/8

Numeric Functions (ROUND, CEIL, FLOOR, ABS)

Part of the SQL Roadmap

In short

Averages come out as 1847.3333333, delivery boxes come in whole numbers, and refunds show up as negatives. Numeric functions shape raw numbers into answers people can use.

Numeric functions round and adjust numbers. ROUND rounds to a set number of decimals, CEIL and FLOOR round up or down to whole numbers, and ABS removes the sign.

The jaadu packs round orange laddoos into a row of small cardboard sweet boxes, the last box only half filled.

Think of it like this

Packing laddoos into boxes

A sweet shop packs 12 laddoos per box. For 50 laddoos you need 5 boxes, rounding up (CEIL), even though the last box isn't full. Only 4 boxes are completely full (FLOOR). The bill rounds to the nearest rupee (ROUND). And whether stock went up or down by 8, the change was 8 (ABS).

Watch it happen

One value, four functions

4.17
0
-4.17
1
4.5
2
4.83
3

1/5Four sample numbers.

The big ideas

What you need to know

ROUND(x, n)

Rounds to n decimals. ROUND(1847.333, 2) is 1847.33. ROUND(x) rounds to a whole number.

CEIL rounds up

Use it for "how many boxes or pages do I need". CEILING is the SQL Server name; PostgreSQL and MySQL accept both.

FLOOR rounds down

Use it for "how many complete units", like full years of age or full boxes.

ABS gives size, not direction

ABS(actual - target) tells you how far off you were, ignoring whether it was over or under.

See it in code

Walk through the code

sql
1SELECT2  product_name,3  quantity,4  CEIL(quantity / 12.0)              AS boxes_needed,5  FLOOR(quantity / 12.0)             AS full_boxes,6  ROUND(price * 1.18, 2)             AS price_with_gst,7  ROUND(price, -2)                   AS price_nearest_100,8  ABS(stock_now - stock_last_week)   AS stock_change9FROM inventory;10 11-- PostgreSQL: AVG of a double needs a cast before ROUND(x, 2)12SELECT ROUND(AVG(rating)::numeric, 1) AS avg_rating FROM reviews;

1/4Divide by 12.0, not 12, so integer division doesn't drop the remainder before CEIL sees it.

Runs in PostgreSQL and MySQL. In PostgreSQL, ROUND with a decimal count needs a NUMERIC input, so cast float columns first. MySQL doesn't need the cast; write CAST(AVG(rating) AS DECIMAL(4,1)) if you want one.

Save yourself the pain

Common mistakes

Oops

CEIL(quantity / 12) on an integer column in PostgreSQL.

Do this instead

50 / 12 is already 4 before CEIL runs. Use 12.0 so the division keeps decimals.

Oops

ROUND(some_double_column, 2) in PostgreSQL and getting "function round(double precision, integer) does not exist".

Do this instead

Cast first: ROUND(some_double_column::numeric, 2).

Your mission

Boxes and bills

~20 min

Quick check

An order has 25 items and each carton holds 12. Which expression gives the number of cartons needed?

Go deeper

  1. 1Round an average price to 2 decimal places.
  2. 2Watch out for integer division, where 5 / 2 can return 2 in some databases.
  3. 3Use ABS to find the size of a difference regardless of direction.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge