SQL Tutorial SQL Advanced SQL Database SQL References

MySQL MOD() Function



The MySQL MOD() function returns the remainder of x divided by y. In special cases it returns the following:

  • If the number y is 0, then NULL is returned.
  • If x or y or both are NULL, then NULL is returned.

Syntax

/* version 1 */
MOD(x, y)

/* version 2 */
x MOD y

/* version 3 */
x % y

Parameters

x Required. Specify the value that will be divided by y.
y Required. Specify the value that will be divided into x.

Return Value

Returns the remainder of x divided by y.

Example 1:

The example below shows the usage of MOD() function.

mysql> SELECT MOD(12, 3);
Result: 0

mysql> SELECT MOD(14, 3);
Result: 2

mysql> SELECT MOD(13.5, 3.1);
Result: 1.1

mysql> SELECT MOD(13.5, -3.1);
Result: 1.1

mysql> SELECT 10 MOD 7;
Result: 3

mysql> SELECT 10 % 7;
Result: 3

mysql> SELECT MOD(13, 0);
Result: NULL

Example 2:

Consider a database table called Sample with the following records:

Dataxy
Data 1105
Data 2206
Data 3307
Data 4408
Data 5509

To calculate the remainder of division operation, where records of column x is divided by records of column y, the following query can be used:

SELECT *, MOD(x, y) AS MOD_Value FROM Sample;

This will produce the result as shown below:

DataxyMOD_Value
Data 11050
Data 22062
Data 33072
Data 44080
Data 55095

❮ MySQL Functions