Skip to content
Back to 50 Concepts
Topic 24 of 50foundationsBeginner

# 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.

Relational Invariant:Deterministic relational algebra contract evaluated by the Cost-Based Optimizer.

2. Explain Like I'm 5

Intuitive mental model for beginners

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 Trace
Interactive Engine Simulation

Numeric Functions Visual Simulator

Step 1 of 3
SQL>SELECT item_id, price, ROUND(price * 0.95, 2) AS discounted_price FROM inventory;Active Input

Step 1: Input & Parsing Stage

Engine Action: Validate SQL syntax and resolve schema metadata for Numeric Functions
SQL Input

Initiating Numeric Functions

Schema Catalog

Checking Table Invariants

How the Database Executes This:

The database engine inspects active tables, verifies permissions, and prepares the physical operation.

💡 Bank cash drawer: Rounding ensures pennies are distributed accurately without lost fractions.

4. Real-World Example

Calculating payment splits, fee rounding, and inventory batch distributions.

Price Calculations
Raw PriceROUND(..., 2)CEIL(...)FLOOR(...)
45.67845.684645
10.12110.121110
Relational walkthrough: ROUND rounds to the nearest decimal; CEIL always rounds up; FLOOR always rounds down.

5. SQL Syntax

ANSI SQL / PostgreSQL Dialect
Step 1: Rounding and ceiling functions
SELECT
  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

RuleBehavioral Explanation
ROUND PrecisionROUND(num, s) rounds to s decimal places. Omission of s defaults to 0 decimal places.
CEIL vs FLOORCEIL rounds towards positive infinity; FLOOR rounds towards negative infinity.
NUMERIC ExactnessNUMERIC/DECIMAL provides arbitrary precision; FLOAT/REAL introduces floating point inaccuracy.
Exact vs Approximate numeric precisionNUMERIC/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 );
Caution: Never use FLOAT for financial currency balances or accounting books.

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

Scenario Question:

What does CEIL(8.1) evaluate to in SQL?

Select your answer: