How MOD() Works in MariaDB

In MariaDB, MOD() is a built-in function that returns the modulo operation. It returns the remainder of its first argument divided by its second argument.

Syntax

The syntax goes like this:

MOD(N,M)

The function returns the remainder of N divided by M.

The operation can also be done using the modulo operator, so you can also use the following syntaxes to achieve the same outcome:

N % M
N MOD M

Example

Here’s an example to demonstrate:

SELECT MOD(9, 2);

Result:

+-----------+
| MOD(9, 2) |
+-----------+
|         1 |
+-----------+

Here are some more:

SELECT 
    MOD(8, 2),
    MOD(134, 6),
    MOD(9, 5),
    MOD(9, 10);

Result:

+-----------+-------------+-----------+------------+
| MOD(8, 2) | MOD(134, 6) | MOD(9, 5) | MOD(9, 10) |
+-----------+-------------+-----------+------------+
|         0 |           2 |         4 |          9 |
+-----------+-------------+-----------+------------+

Compared with the Modulo Operator

Here’s an example that compares the syntaxes across the function and operators:

SELECT 
    MOD(9, 2),
    9 % 2,
    9 MOD 2;

Result:

+-----------+-------+---------+
| MOD(9, 2) | 9 % 2 | 9 MOD 2 |
+-----------+-------+---------+
|         1 |     1 |       1 |
+-----------+-------+---------+

Non-Numeric Arguments

Here’s an example of what happens when we provide non-numeric arguments:

SELECT MOD('Homer', 'Symptom');

Result:

+-------------------------+
| MOD('Homer', 'Symptom') |
+-------------------------+
|                    NULL |
+-------------------------+
1 row in set, 3 warnings (0.005 sec)

Let’s see the warning:

SHOW WARNINGS;

Result:

+---------+------+---------------------------------------------+
| Level   | Code | Message                                     |
+---------+------+---------------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: 'Homer'   |
| Warning | 1292 | Truncated incorrect DOUBLE value: 'Symptom' |
| Warning | 1365 | Division by 0                               |
+---------+------+---------------------------------------------+

Null Arguments

MOD() returns null if either argument is null:

SELECT 
    MOD(null, 2),
    MOD(9, null),
    MOD(null, null);

Result:

+--------------+--------------+-----------------+
| MOD(null, 2) | MOD(9, null) | MOD(null, null) |
+--------------+--------------+-----------------+
|         NULL |         NULL |            NULL |
+--------------+--------------+-----------------+

Missing Arguments

Calling MOD() with the wrong number of arguments, or without any arguments results in an error:

SELECT MOD();

Result:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1

And:

SELECT MOD(10, 2, 3);

Result:

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' 3)' at line 1