0
0
MySQLquery~5 mins

ABS and MOD in MySQL

Choose your learning style9 modes available
Introduction
ABS gives the positive value of a number, and MOD finds the remainder after division. They help with simple math tasks in databases.
When you want to ignore negative signs and get only positive numbers.
When you need to find out what remains after dividing one number by another.
When calculating distances or differences where only positive values make sense.
When checking if a number is even or odd using the remainder.
When splitting items into groups and seeing how many are left over.
Syntax
MySQL
ABS(number)
MOD(number, divisor)
ABS takes one number and returns its positive value.
MOD takes two numbers: the number and the divisor, and returns the remainder.
Examples
Returns 10 because ABS removes the negative sign.
MySQL
SELECT ABS(-10);
Returns 1 because 10 divided by 3 leaves a remainder of 1.
MySQL
SELECT MOD(10, 3);
Returns 5 because the number is already positive.
MySQL
SELECT ABS(5);
Returns 4 because 14 divided by 5 leaves a remainder of 4.
MySQL
SELECT MOD(14, 5);
Sample Program
This query shows how ABS converts -25 to 25 and MOD finds the remainder when 25 is divided by 4.
MySQL
SELECT ABS(-25) AS positive_value, MOD(25, 4) AS remainder;
OutputSuccess
Important Notes
ABS always returns a positive number or zero.
MOD returns zero if the first number is exactly divisible by the second.
If you use MOD with zero as divisor, it will cause an error.
Summary
ABS returns the positive value of any number.
MOD returns the remainder after division.
Both are useful for simple math operations in SQL queries.