Numeric Functions: ROUND, ABS & More
Round strike rates with ROUND, remove minus signs with ABS, and learn CEILING, FLOOR, POWER, SQRT and the integer-division trap.
Strike rate = runs × 100 ÷ balls. For Virat Kohli that is 144.23076923… — nobody wants 14 decimals on a scoreboard. Numeric functions shape numbers for reports.
Definition
A numeric function is a single-row function that takes a number as input and returns a number — rounded, made positive, raised to a power, and so on.
| Function | Definition |
|---|---|
ROUND(number, decimals) | Rounds a number to the given number of decimal places. Digits 5–9 round up, 0–4 round down. |
ABS(number) | Returns the absolute value — the number without its minus sign (its distance from zero). |
CEILING(number) | Smallest whole number greater than or equal to the value (always rounds up). |
FLOOR(number) | Largest whole number less than or equal to the value (always rounds down). |
POWER(x, y) | x raised to the power y. |
SQRT(x) / SQUARE(x) | Square root / x × x. |
Syntax
sql
ROUND(number, decimal_places)
ABS(number)
CEILING(number)
FLOOR(number)Example — ROUND and ABS on literal values
sql
SELECT
ROUND(3.516, 2) AS r2,
ROUND(3.516, 1) AS r1,
ROUND(3.516, 0) AS r0,
ABS(-2.5) AS abs_neg,
ABS(2.5) AS abs_pos,
CEILING(3.2) AS ceil,
FLOOR(3.8) AS flr,
POWER(2, 3) AS pwr,
SQRT(16) AS root;| r2 | r1 | r0 | abs_neg | abs_pos | ceil | flr | pwr | root |
|---|---|---|---|---|---|---|---|---|
| 3.52 | 3.5 | 4 | 2.5 | 2.5 | 4 | 3 | 8 | 4 |
Note: SQL Server keeps the same number of decimal places as the input, so it displays
ROUND(3.516, 2)as 3.520,ROUND(3.516, 1)as 3.500 andROUND(3.516, 0)as 4.000. The value is the same — only the display differs from the playground.
ABS is handy for negative values like a net run rate of −0.45 or "how far is each player from 500 runs", where only the size of the gap matters.
Example — strike rate with ROUND
Our player_profiles table has both runs and balls (the PDF's chapter-7 players table).
sql
SELECT
name,
runs,
balls,
ROUND(runs * 100.0 / balls, 2) AS strike_rate
FROM player_profiles;| name | runs | balls | strike_rate |
|---|---|---|---|
| Virat Kohli | 750 | 520 | 144.23 |
| Rohit Sharma | 500 | 390 | 128.21 |
| Jos Buttler | 850 | 560 | 151.79 |
| MS Dhoni | 350 | 215 | 162.79 |
| David Warner | 700 | 505 | 138.61 |
| Jasprit Bumrah | 20 | 12 | 166.67 |
How it works: step 1 — divide runs by balls and multiply by 100 (strike rate = runs per 100 balls); step 2 — ROUND to 2 decimals. In SQL Server the column shows trailing zeros (e.g. 144.230000); wrap it in CAST(... AS DECIMAL(6,2)) for a clean 144.23.
The integer-division trap — why 100.0, not 100
When both sides of / are integers, SQL Server does integer division and throws away the decimals: 7 / 2 = 3, but 7 / 2.0 = 3.5.
sql
SELECT
name,
runs * 100 / balls AS int_sr,
runs * 100.0 / balls AS decimal_sr
FROM player_profiles;| name | int_sr | decimal_sr |
|---|---|---|
| Virat Kohli | 144 | 144.23076923076923 |
| Rohit Sharma | 128 | 128.2051282051282 |
| Jos Buttler | 151 | 151.78571428571428 |
| MS Dhoni | 162 | 162.7906976744186 |
| David Warner | 138 | 138.6138613861386 |
| Jasprit Bumrah | 166 | 166.66666666666666 |
Writing 100.0 makes the expression decimal, so the fractional part survives until ROUND decides what to keep.
Example — ABS for "distance from 500 runs"
sql
SELECT
name,
runs,
runs - 500 AS diff,
ABS(runs - 500) AS gap
FROM players;| name | runs | diff | gap |
|---|---|---|---|
| Virat Kohli | 750 | 250 | 250 |
| Rohit Sharma | 500 | 0 | 0 |
| Jos Buttler | 850 | 350 | 350 |
| MS Dhoni | 350 | -150 | 150 |
| David Warner | 700 | 200 | 200 |
| Jasprit Bumrah | 20 | -480 | 480 |
Common mistakes
- ❌
runs * 100 / balls→ integer result (144). ✅runs * 100.0 / balls→ 144.23… - ❌ Expecting
CAST(3.516 AS INT)to round. ✅ It truncates → 3. UseROUNDto round. - ❌ Using
ROUNDto always round up. ✅ROUNDgoes to the nearest value; useCEILINGto always round up andFLOORto always round down. - ❌ Dividing by a column that can be 0 → "Divide by zero error" in SQL Server. ✅
NULLIF(balls, 0)(see NULL functions).
SQL Server vs MySQL / PostgreSQL
| Task | SQL Server | MySQL | PostgreSQL |
|---|---|---|---|
| Round up | CEILING(x) | CEILING(x) / CEIL(x) | CEILING(x) / CEIL(x) |
| Integer ÷ integer | integer result | decimal result (7/2 = 3.5000) | integer result |
Tip:
ROUNDalso accepts a negative length in SQL Server:ROUND(750, -2)= 800 (nearest hundred). The browser playground ignores negative lengths, so to try "nearest hundred" there useROUND(runs / 100.0, 0) * 100.
Interview tip
Q: Why does SELECT 750 / 520 return 1 in SQL Server?
Both operands are integers, so SQL Server performs integer division and discards the remainder. Make one operand decimal (750 * 1.0 / 520 or CAST(750 AS DECIMAL(10,2)) / 520) to get 1.44.
Key points to remember
- ROUND(number, decimals) rounds to the nearest value: 5–9 round up, 0–4 round down.
- SQL Server keeps the input's decimal places, so ROUND(3.516, 2) displays as 3.520.
- ABS removes the minus sign — useful for gaps, differences and negative net run rates.
- CEILING always rounds up, FLOOR always rounds down.
- Integer ÷ integer is integer division in SQL Server; multiply by 100.0 (not 100) to keep decimals.
- Strike rate = ROUND(runs * 100.0 / balls, 2).
Try it yourself
ROUND, ABS, CEILING and FLOOR
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Strike rate rounded to 2 decimals
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
SQL Server only: negative ROUND length and clean decimals
SELECT
name,
ROUND(runs, -2) AS nearest_hundred, -- 750 -> 800
CAST(ROUND(runs * 100.0 / balls, 2) AS DECIMAL(6,2)) AS strike_rate -- 144.23
FROM player_profiles;SQL Server-specific — run this in SQL Server (SSMS / Azure Data Studio).
Practice: runs per ball
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
