← Back to Chapters

MySQL Numeric Functions

? MySQL Numeric Functions

? Quick Overview

MySQL Numeric Functions are built-in functions used to perform calculations, rounding, absolute values, random numbers, and mathematical operations on numeric data. They are commonly used in reports, analytics, billing systems, and financial calculations.

? Key Concepts

  • Work with integers and decimal numbers.
  • Used inside SELECT, WHERE, and expressions.
  • Helpful for calculations and data analysis.
  • Can be combined with other SQL functions.

? Common Numeric Functions

  • ABS(): Returns the absolute value.
  • CEILING() / CEIL(): Rounds up to the nearest integer.
  • FLOOR(): Rounds down to the nearest integer.
  • ROUND(): Rounds to a specified number of decimal places.
  • TRUNCATE(): Truncates to a specified number of decimal places.
  • MOD(): Returns the remainder of division.
  • POWER(): Returns the value of a number raised to the power of another number.
  • SQRT(): Returns the square root of a number.
  • RAND(): Returns a random floating-point value between 0 and 1.

? Syntax & Theory

Numeric functions take numeric input and return a calculated numeric result. They can be applied directly to values or table columns.

? Code Examples

? View Code Example
// Absolute value removes negative sign
SELECT ABS(-25);
? View Code Example
// Rounds number to nearest integer
SELECT ROUND(12.6);
? View Code Example
// Always rounds up to next integer
SELECT CEILING(4.2);
? View Code Example
// Always rounds down to previous integer
SELECT FLOOR(9.9);
? View Code Example
// Returns remainder of division (10 divided by 3)
SELECT MOD(10,3);
? View Code Example
// Power calculation (2 to the power of 3)
SELECT POWER(2,3);
? View Code Example
// Square root of a number
SELECT SQRT(81);
? View Code Example
// Generates random number between 0 and 1
SELECT RAND();
? View Code Example
// Truncate decimal without rounding
SELECT TRUNCATE(12.99, 0);

? Output & Explanation

Live Results

  • ABS(-25)25
  • ROUND(12.6)13
  • CEILING(4.2)5
  • FLOOR(9.9)9
  • MOD(10,3)1
  • POWER(2,3)8
  • SQRT(81)9
  • RAND()0.843... (Random decimal value)
  • TRUNCATE(12.99, 0)12

✅ Tips & Best Practices

  • Use ROUND() when displaying prices or averages to keep data clean.
  • Prefer TRUNCATE() when decimals must be removed strictly without rounding up.
  • Combine numeric functions with WHERE clauses carefully for filtering data.
  • Test calculations with sample data before production use to ensure accuracy.

? Try It Yourself

  • Find rounded salary values from an employee table.
  • Generate random discount values using RAND().
  • Calculate square roots of stored numeric columns.
  • Use MOD() to find even and odd numbers (e.g., id % 2 = 0).