MySQL MOD Function

MySQL MOD function is one of the Mathematical methods that help find the Modulus. Or say it finds the remainder of different values. The syntax of the MOD function is as shown below:

SELECT MOD(X, Y);

SELECT X % Y;

SELECT X MOD Y;

It divides the X with the Y value and returns the remainder.

MySQL MOD Function Example

The MOD function returns the remainder or Modulus of the given value. The following query shows multiple ways to use this. Also, read about the MySQL CRC32 function.

SELECT MOD(25, 5);

SELECT MOD(25, 4), MOD(29, 9);

SELECT 29 MOD 9, 28 MOD 5, 29 % 5;
MOD Function Example 1

This Mathematical method also allows you to find the Modulus of column data. In this MySQL example, we are going to find the Modulus of the Yearly Income column divided by 9. Check out the MySQL CONV function.

TIP: Please refer to the MySQL RAND and MySQL PI functions from the available list of MySQL Numeric Functions.

SELECT EmpID, 
       FirstName,
       LastName,
       Occupation,
       YearlyIncome,
       MOD(YearlyIncome, 9) AS Mod9Income,
       Sales,
       Sales MOD 5 AS Mod5Sales
 FROM customer;
MOD Function Example 2