MySQL MOD() Function

The MOD() function in MySQL calculates the remainder after a division operation. This is a fundamental mathematical operation useful in various programming and data manipulation tasks.



MOD(): Definition and Usage

MOD() takes two numbers as input: the dividend (the number being divided) and the divisor (the number by which you are dividing). It returns the remainder—the part left over after the division is complete. This is different from standard division (using `/`), which returns the quotient (the result of the division).

Syntax

There are three equivalent ways to write the MOD() function:

Syntax

MOD(x, y)
--or--
x MOD y
--or--
x % y
      

Parameter Values

Parameter Description
x The dividend (the number being divided). This is required.
y The divisor (the number you divide by). This is required.

Examples

Calculating the Remainder

This example finds the remainder when 18 is divided by 4.

Syntax

SELECT MOD(18, 4);
      
Output

2
      

Alternative Syntaxes

These examples demonstrate the other two equivalent syntaxes for MOD().

Syntax

SELECT 18 MOD 4;
SELECT 18 % 4;
      
Output

2
2