MOD()
Returns the remainder of a division (modulo).
Description
MOD returns the remainder after dividing one number by another (the modulo
operation). It's the classic tool for "every Nth row", checking even/odd, or
wrapping counters. MOD(10, 3) is 1.
Syntax
MOD(dividend, divisor)
dividend % divisorParameters
| Name | Description | Optional |
|---|---|---|
| dividend | The value to divide. | No |
| divisor | The value to divide by. | No |
Return Type
Returns the same numeric type as the inputs.
Examples
Employees & Departments
Loading database engine...
Employees & Departments
Loading database engine...
Even or odd
MOD(id, 2) = 0 means even; = 1 means odd. Pair with WHERE to grab every
other row.
Common Mistakes
- Dividing by zero.
MOD(x, 0)errors — guard the divisor. - Sign of the result. The sign follows the dividend;
MOD(-17, 5)is-2in most SQL engines. - Confusing with division.
MODgives the leftover, not the quotient.
Related Functions
See also: FLOOR, ABS.