Mysql Mod Function

MySQL: MOD Function

This tutorial explains how to use the MySQL MOD function with syntax and examples.

Description

The MySQL MOD function returns the remainder of n divided by m.

Syntax

The syntax for the MOD function in MySQL is:

MOD( n, m )

OR

n MOD m

OR

n % m

Parameters or Arguments

n

The value that will be divided by m.

m

The value that will be divided into n.

Note

  • The MOD function uses the formula of n / m and the remainder is what is returned.
  • Starting in MySQL 4.1.7, the MOD function returns the exact remainder (without any rounding).
  • Before MySQL 4.1.7, the MOD function rounds the remainder to an integer value.
  • The MOD function syntax of n mod m was introduced in MySQL 4.1.

Applies To

The MOD function can be used in the following versions of MySQL:

  • MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0, MySQL 3.23

Example

Let's look at some MySQL MOD function examples and explore how to use the MOD function in MySQL.

For example:

mysql> SELECT MOD(12, 5);
Output: 2

mysql> SELECT MOD(12, 0.18);
Output: 0.12

mysql> SELECT MOD(100, 3.5938);
Output: 2.9674
mysql> SELECT 12 MOD 5;
Output: 2

mysql> SELECT 12 MOD 0.18;
Output: 0.12

mysql> SELECT 100 MOD 3.5938;
Output: 2.9674
mysql> SELECT 12 % 5;
Output: 2

mysql> SELECT 12 % 0.18;
Output: 0.12

mysql> SELECT 100 % 3.5938;
Output: 2.9674