← Back to MySQL Course | Chapter 13: Date & Numeric Functions | Lesson 2 of 6

ABS & MOD

ABS is like ignoring whether you walked forward or backward and only counting the distance. MOD gives what is left over after dividing.
Syntax
sql
ABS(number)
MOD(dividend, divisor)

The Absolute Value with ABS

ABS converts any negative number into its positive equivalent while leaving positive numbers unchanged, which is useful for measuring the size of a difference between two values without caring which one is larger.

Example: The Absolute Value with ABS

sql
SELECT ABS(-15) AS positive_value;

The Remainder with MOD

MOD returns the remainder left over after dividing one number by another, the same operation as the % operator in most programming languages — useful for tasks like distributing rows evenly across a fixed number of groups.

Example: The Remainder with MOD

sql
SELECT MOD(17, 5) AS remainder;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Finding Even and Odd Numbers

Checking whether a number is even or odd is a classic use of MOD: dividing by 2 and testing whether the remainder is 0 tells you evenness instantly, without writing a more complex conditional.

Example: Finding Even and Odd Numbers

sql
CREATE TABLE numbers (n INT);
INSERT INTO numbers VALUES (4), (7), (10);
SELECT n, CASE WHEN MOD(n, 2) = 0 THEN 'even' ELSE 'odd' END AS parity FROM numbers;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Using MOD with Decimal Values

MOD isn't limited to whole numbers — it also works on decimal values, dividing the first number by the second and returning the leftover floating-point remainder rather than an error.

Example: Using MOD with Decimal Values

sql
SELECT MOD(10.5, 3) AS decimal_remainder;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Combining ABS and MOD

Combining ABS and MOD together guarantees a positive remainder even when working with negative inputs, since MOD alone can return a negative result if the dividend is negative.

Example: Combining ABS and MOD

sql
SELECT MOD(-7, 3) AS raw_mod, ABS(MOD(-7, 3)) AS positive_mod;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting MOD of a negative number to be positive, when MOD(-7, 3) is -1.
  2. Using ABS on a NULL, which returns NULL.
  3. Dividing by zero with MOD(x, 0), which returns NULL.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.