NumbersSQL standard

MOD

Returns the remainder of dividing one number by another.

Syntax

MOD(<dividend>, <divisor>)
<dividend> % <divisor>

MOD(10, 3) gives 1, because 10 = 3 · 3 + 1. The % operator does the same. Most often it's used to check whether a number is even, take every second row or deal rows into several groups in turn.

Data for the examples

orders

idcreated_atamount
12026-07-03 10:15:00120.50
22026-07-28 18:40:0080.25
32026-08-09 09:05:00199.99
42026-08-21 14:30:0045.00
52026-09-02 11:00:0064.75

These tables already exist in the sandbox — the examples below use them. Want to try this in your own database? Copy the SQL.

The remainder of a division
SELECT MOD(10, 3) AS remainder,
10 % 3 AS with_operator;
remainderwith_operator
11
Even order numbers
SELECT id,
MOD(id, 2) = 0 AS is_even
FROM orders;
idis_even
1false
2true
3false
4true
5false
Every second order
SELECT id, amount
FROM orders
WHERE MOD(id, 2) = 0;
idamount
280.25
445.00

Watch out

For a negative dividend, the remainder is negative too: MOD(-7, 3) gives -1, not 2. If you always need a non-negative one, use MOD(MOD(-7, 3) + 3, 3).

Division by zero is an error: MOD(5, 0) says division by zero.

It works for fractional numbers too: MOD(7.5, 2) gives 1.5.

Related functions