Numeric & Math Functions (ROUND, CEIL, FLOOR, ABS, MOD, POWER)
Round, bucket, and transform numbers for reporting and feature engineering.
In plain words
Math functions round numbers, remove signs, find remainders and group numbers into ranges.
Think of it like…
A shopkeeper's tools: rounding a bill to the nearest rupee (ROUND), packing items into boxes of ten and counting full boxes (FLOOR of a division), and counting what's left over (MOD).
Where you’ve seen it
Price bands for a report: SELECT FLOOR(price / 500) * 500 AS band_start, COUNT(*) FROM products GROUP BY 1 ORDER BY 1; puts ₹0–499 in band 0, ₹500–999 in band 500, and so on.
Words you’ll hear
- ROUND(x, n)
- Rounds to n decimal places; tie behaviour depends on the type.
- CEIL / FLOOR
- Round up / down to the nearest integer (FLOOR(-2.5) = -3).
- MOD(a, b) / %
- Remainder of a ÷ b; its sign follows a.
- WIDTH_BUCKET
- Assigns a value to one of N equal-width buckets (PostgreSQL, Oracle).
Visual diagram
Theory & example queries
Rounding correctly
ROUND(x, n) rounds to n decimal places; n can be negative (ROUND(1249, -2) = 1200). In PostgreSQL, rounding a numeric value breaks ties away from zero (2.5 → 3, -2.5 → -3), while rounding a double precision value usually rounds half to even (2.5 → 2), so the same-looking query can give different answers depending on the column type. Money should be numeric for this reason too.
CEIL rounds up and FLOOR rounds down, towards positive and negative infinity respectively, so FLOOR(-2.5) is -3 and CEIL(-2.5) is -2, while TRUNC(-2.5) is -2 because it simply drops the decimals. Rounding each line of an invoice and then summing can differ from rounding the total, so decide which one the business rule wants.
SELECT ROUND(2.5::numeric) AS numeric_round, ROUND(2.5::float8) AS float_round, FLOOR(-2.5) AS floor_neg, TRUNC(-2.5) AS trunc_neg;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
PostgreSQL result1 sample row
| numeric_round | float_round | floor_neg | trunc_neg |
|---|---|---|---|
| 3 | 2 | -3 | -2 |
Note: Values follow the PostgreSQL numeric-versus-double rounding rules described in the lesson; another database may use different tie behavior.
returns 3, 2, -3 and -2 in PostgreSQL.
Key takeaway: Know your type's tie rule; FLOOR and TRUNC differ for negatives.
Bucketing, remainders and other helpers
To group values into fixed-width ranges, divide, floor and multiply back: FLOOR(amount / 500) * 500 gives the start of each ₹500 band. PostgreSQL's WIDTH_BUCKET(amount, 0, 5000, 10) assigns values to 10 equal buckets between 0 and 5,000, with out-of-range values in bucket 0 or 11. MOD gives remainders, useful for every-nth-row sampling (WHERE MOD(id, 10) = 0) and cycles; its result has the sign of the dividend, so MOD(-7, 3) is -1.
GREATEST and LEAST pick the largest or smallest of several values; in PostgreSQL they ignore NULLs, while in MySQL any NULL argument makes the result NULL. When percentages are rounded individually they often add up to 99 or 101; if a report must total 100, round the parts and give the remainder to the largest one.
| id | customer_id | status | amount | coupon_code |
|---|---|---|---|---|
| 101 | 1 | paid | 249 | NULL |
| 102 | 2 | paid | 120 | WELCOME10 |
| 103 | 1 | shipped | 89.5 | NULL |
SELECT WIDTH_BUCKET(amount, 0, 5000, 5) AS bucket, MIN(amount), MAX(amount), COUNT(*)FROM ordersGROUP BY 1ORDER BY 1;PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Representative order bands4 sample rows
| bucket | min | max | count |
|---|---|---|---|
| 1 | 250 | 950 | 3 |
| 2 | 1100 | 1900 | 4 |
| 3 | 2250 | 2950 | 2 |
| 4 | 3100 | 4900 | 3 |
Note: Sample amounts are illustrative. WIDTH_BUCKET returns 0 or 6 for values outside the 0–5000 range, so real output can contain those extra groups.
Key takeaway: FLOOR(x / width) * width or WIDTH_BUCKET for bands; MOD for remainders.
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Check the column type
Numeric and floating-point values round differently, and integers divide without decimals.
amount numeric(12,2) vs ratio float8
When to use it & trade-offs
What it is
SQL's math functions shape numbers for reports and analysis. ROUND(x, n) rounds to n decimal places, CEIL and FLOOR round up or down to whole numbers, TRUNC cuts off decimals, ABS removes the sign, MOD (or %) gives the remainder, and POWER, SQRT, LN and LOG do the rest. Together with integer division, they're also the standard tools for bucketing values into ranges.
Why it matters: Price bands, rounding for invoices, percentages that should add up to 100, every-nth-row sampling: small numeric details decide whether totals reconcile. Rounding rules differ between data types and databases, which is a classic source of off-by-a-paisa bugs.
When to reach for it
- Price and amount bands in reports
- Rounding money and percentages for display
- Sampling every nth row with MOD
When to avoid or be careful
- Rounding floating-point money values
- Rounding every line and the total separately without deciding which is authoritative
- Assuming FLOOR and TRUNC are the same for negative numbers
ROUND vs TRUNC vs FLOOR/CEIL
Strengths: Nearest value; supports decimal places.
Watch out: Tie rules differ between numeric and float types.
Strengths: Predictable: just cuts digits.
Watch out: Always moves towards zero, which biases totals.
Strengths: Consistent direction, including for negatives.
Watch out: Only to whole numbers (scale first for decimals).
Interview signals & numbers to know
When this topic comes up
- “Group prices into ranges”
- “The total is off by one paisa”
- “Percentages add up to 101%”
- Rounding rules
- Feature engineering
Benchmarks & rules of thumb
- ROUND(2.5::numeric)
- = 3
- FLOOR(-2.5)
- = -3; TRUNC(-2.5) = -2
- MOD(-7, 3)
- = -1
Common mistakes & follow-ups
Common interview mistakes
- Using FLOOR on negative amounts (refunds) and getting bands one lower than expected.
- Getting 0% everywhere because ROUND(100 * a / b) used integer division.
- Expecting GREATEST(a, NULL) to return a in MySQL; it returns NULL.
Likely interviewer follow-ups
- How would you group order amounts into ₹500 bands?
- Why might ROUND(2.5) return 2 in one query and 3 in another?
- How do you make rounded percentages add up to 100?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Numeric & Math Functions (ROUND, CEIL, FLOOR, ABS, MOD, POWER) with hands-on SQL practice and interview Q&A.