# Topic 24: Numeric Functions
Mathematical operations: ROUND, CEIL, FLOOR, ABS, MOD, and floating-point vs exact numeric precision.
1. Definition
SQL numeric functions perform mathematical computations, rounding, truncation, and absolute value evaluations on integer and decimal columns.
2. Explain Like I'm 5
Numeric functions are the buttons on a scientific calculator: rounding money to 2 decimals, rounding up with ceiling, or getting positive numbers with absolute value.
3. Animation & Visual Simulation
Interactive Engine TraceNumeric Functions Visual Simulator
SELECT item_id, price, ROUND(price * 0.95, 2) AS discounted_price
FROM inventory;Active InputStep 1: Input & Parsing Stage
Initiating Numeric Functions
Checking Table Invariants
The database engine inspects active tables, verifies permissions, and prepares the physical operation.
4. Real-World Example
Calculating payment splits, fee rounding, and inventory batch distributions.
| Raw Price | ROUND(..., 2) | CEIL(...) | FLOOR(...) |
|---|---|---|---|
| 45.678 | 45.68 | 46 | 45 |
| 10.121 | 10.12 | 11 | 10 |
5. SQL Syntax
ANSI SQL / PostgreSQL DialectSELECT ROUND(125.4567, 2) AS rounded_cents, -- 125.46 CEIL(4.01) AS ceiling_val, -- 5 FLOOR(4.99) AS floor_val, -- 4 ABS(-50.00) AS absolute_balance, -- 50.00 MOD(10, 3) AS remainder; -- 1
6. Important Rules & Behaviors
| Rule | Behavioral Explanation |
|---|---|
| ROUND Precision | ROUND(num, s) rounds to s decimal places. Omission of s defaults to 0 decimal places. |
| CEIL vs FLOOR | CEIL rounds towards positive infinity; FLOOR rounds towards negative infinity. |
| NUMERIC Exactness | NUMERIC/DECIMAL provides arbitrary precision; FLOAT/REAL introduces floating point inaccuracy. |
| Exact vs Approximate numeric precision | NUMERIC/DECIMAL provides exact arbitrary precision; FLOAT/REAL has binary rounding errors. |
| Rounding semantics (ROUND vs TRUNC) | ROUND rounds to the nearest integer; TRUNC truncates towards zero without rounding. |
7. Deep Dive: Floating Point Rounding Disasters in Financial Systems
Storing money using FLOAT or DOUBLE causes binary IEEE 754 precision loss where 0.1 + 0.2 = 0.30000000000000004. In production banking schemas, ALWAYS use NUMERIC(12, 2) or store integer cents.
-- WRONG (FLOAT causes financial drift): CREATE TABLE bad_ledger ( balance FLOAT ); -- CORRECT (Exact decimal representation): CREATE TABLE good_ledger ( balance NUMERIC(14, 2) NOT NULL DEFAULT 0.00 );
8. Advantages & Disadvantages
Advantages
- •Executes mathematical transformations close to the data.
- •Guarantees uniform rounding across all API consumers.
Disadvantages & Trade-offs
- •Complex trigonometric calculations in SQL consume high database CPU.
9. Common Interview Questions
Q1.Why should financial and monetary calculations NEVER use FLOAT or DOUBLE PRECISION in SQL?
FLOAT and DOUBLE PRECISION use IEEE 754 binary floating-point representation, which cannot accurately represent common base-10 decimals like 0.1 or 0.01 (e.g. 0.1 + 0.2 = 0.30000000000000004). Over millions of financial transactions, accumulated rounding errors cause ledger imbalances. Financial systems must ALWAYS use NUMERIC(precision, scale) or DECIMAL, which store exact base-10 digits.
Q2.What is the difference between CEIL, FLOOR, ROUND, and TRUNC?
CEIL(x) rounds UP to the nearest integer (CEIL(4.1) = 5). FLOOR(x) rounds DOWN to the nearest integer (FLOOR(4.9) = 4). ROUND(x) rounds to the nearest integer (ROUND(4.5) = 5). TRUNC(x) simply discards fractional digits towards zero (TRUNC(4.9) = 4, TRUNC(-4.9) = -4).
Q3.What does NUMERIC(10, 2) mean in relational schema design?
Precision (10) is the total number of significant digits in the number, both left and right of the decimal point. Scale (2) is the number of digits to the right of the decimal point. Therefore, NUMERIC(10, 2) can store numbers from -99,999,999.99 to +99,999,999.99. Inserting a number with more than 8 digits before the decimal point throws a numeric overflow error.
Q4.How does modulus (%) behave with negative numbers in SQL?
In SQL (and PostgreSQL), MOD(n, m) returns the remainder of n / m. The sign of the result matches the sign of the dividend (n): MOD(-7, 3) returns -1, whereas in Python (-7 % 3) returns 2. In cross-platform applications, always wrap negative modulus math in ABS() if positive remainders are expected.
Q5.What is Banker’s Rounding (Round to Even) and how does SQL handle midpoint rounding?
Standard math rounds midpoints (0.5) up. Banker’s Rounding rounds midpoints to the nearest EVEN number (2.5 -> 2, 3.5 -> 4) to eliminate statistical positive bias in large financial datasets. Standard SQL ROUND() typically rounds half-away-from-zero (4.5 -> 5, -4.5 -> -5), but floating-point casts can trigger IEEE round-to-even behavior.
10. Interview-Ready Answer
“SQL provides built-in numeric functions like ROUND, CEIL, FLOOR, and MOD. For financial applications, storing amounts in NUMERIC guarantees exact precision and eliminates floating-point rounding discrepancies.”
11. Quick Test
What does CEIL(8.1) evaluate to in SQL?