MySQL

Numeric Function


MySQL provides several numeric functions that allow you to perform mathematical calculations on numeric values. These functions help with operations like rounding, modulus, absolute values, and more.

 

 

1. ABS()

The ABS() function returns the absolute (positive) value of a number.

Syntax:

SELECT ABS(number);
 

Example:

SELECT ABS(-42) AS AbsoluteValue;
 

Result:

AbsoluteValue
42

 

 

 

2. CEIL() / CEILING()

The CEIL() or CEILING() function rounds a number up to the nearest integer.

Syntax:

SELECT CEIL(number);
SELECT CEILING(number);
 

Example:

SELECT CEIL(4.2) AS CeilValue;

 

Result:

CeilValue
5

 

 

 

3. FLOOR()

The FLOOR() function rounds a number down to the nearest integer.

Syntax:

SELECT FLOOR(number);
 

Example:

SELECT FLOOR(4.8) AS FloorValue;
 

Result:

FloorValue
4

 

 

4. ROUND()

The ROUND() function rounds a number to a specified number of decimal places.

Syntax:

SELECT ROUND(number, decimal_places);
 

Example:

SELECT ROUND(3.14159, 2) AS RoundedValue;
 

Result:

RoundedValue
3.14

 

 

5. MOD()

The MOD() function returns the remainder of the division of one number by another (modulus).

Syntax:

SELECT MOD(dividend, divisor);
 

Example:

SELECT MOD(10, 3) AS Remainder;

 

Result:

Remainder
1

 

 

Summary of Common Numeric Functions

 

FunctionDescription
ABS()Returns the absolute value of a number.
CEIL()Rounds a number up to the nearest integer.
FLOOR()Rounds a number down to the nearest integer.
ROUND()Rounds a number to a specified number of decimal places.
MOD()Returns the remainder of a division.
POWER()Returns a number raised to the power of another number.
SQRT()Returns the square root of a number.
SIGN()Returns the sign of a number (-1, 0, 1).
RAND()Generates a random floating-point number.
TRUNCATE()Truncates a number to a specified decimal place.
PI()Returns the value of π (3.141593).
EXP()Returns e raised to the power of a given number.
LOG()Returns the natural logarithm (base e) of a number.
LOG10()Returns the base 10 logarithm of a number.
GREATEST()Returns the largest value in a list of values.
LEAST()Returns the smallest value in a list of values.