0
0
SQLquery~5 mins

ABS and MOD functions in SQL

Choose your learning style9 modes available
Introduction
ABS and MOD help you work with numbers by finding the positive value and the remainder after division.
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 splitting items into groups and checking how many are left over.
When working with cycles or repeating patterns in numbers.
Syntax
SQL
ABS(number)
MOD(number, divisor)
ABS returns the positive value of the number, even if it was negative.
MOD returns the remainder after dividing number by divisor.
Examples
Returns 10 because ABS removes the negative sign.
SQL
SELECT ABS(-10);
Returns 1 because 10 divided by 3 leaves a remainder of 1.
SQL
SELECT MOD(10, 3);
Returns 5 because the number is already positive.
SQL
SELECT ABS(5);
Returns 4 because 14 divided by 5 leaves a remainder of 4.
SQL
SELECT MOD(14, 5);
Sample Program
This query shows the absolute value of -25 and the remainder when 25 is divided by 4.
SQL
SELECT ABS(-25) AS absolute_value, MOD(25, 4) AS remainder;
OutputSuccess
Important Notes
ABS always returns a non-negative number.
MOD result is always less than the divisor and has the same sign as the dividend in some SQL dialects.
If divisor is zero in MOD, it will cause an error.
Summary
ABS gives the positive version of any number.
MOD finds the leftover part after division.
Both are useful for handling numbers in everyday tasks.