MySQL MOD() Function – Perform a Modulo Operation in MySQL

In MySQL, the MOD() function performs a modulo operation. It returns the remainder of a number divided by another number.

You provide the two values as arguments when calling the function.

Syntax

This function supports the following three syntaxes.

MOD(N,M)
N % M
N MOD M

The function returns the remainder of N divided by M.

Both arguments can have a fractional part, and they can also be a BIGINT data type.

Example 1 – First Syntax

Here’s a basic example to demonstrate the first syntax.

SELECT MOD(10, 3);

Result:

+------------+
| MOD(10, 3) |
+------------+
|          1 |
+------------+

Example 2 – Second Syntax

Here’s the same example except using the second syntax.

SELECT 10 % 3;

Result:

+--------+
| 10 % 3 |
+--------+
|      1 |
+--------+

Example 3 – Third Syntax

And here’s the same example again using the third syntax.

SELECT 10 MOD 3;

Result:

+----------+
| 10 MOD 3 |
+----------+
|        1 |
+----------+

Example 4 – Fractions

You can also use values that have a fractional part.

SELECT 
  MOD(10, 5.3),
  MOD(10.3, 5);

Result:

+--------------+--------------+
| MOD(10, 5.3) | MOD(10.3, 5) |
+--------------+--------------+
|          4.7 |          0.3 |
+--------------+--------------+

Example 5 – Dividing by Zero

Dividing a number by zero returns NULL.

SELECT 10 MOD 0;

Result:

+----------+
| 10 MOD 0 |
+----------+
|     NULL |
+----------+

Example 6 – Dividing a Zero Value

However, dividing a zero value will simply result in zero.

SELECT 0 MOD 10;

Result:

+----------+
| 0 MOD 10 |
+----------+
|        0 |
+----------+