PostgreSQL Tutorial: MOD Function

August 7, 2023

Summary: The PostgreSQL MOD() function performs the modulo operation that returns the remainder after the division of the first argument by the second one.

Table of Contents

Syntax

The syntax of the PostgreSQL MOD() function is as follows:

MOD(x,y)

Arguments

The MOD() function requires two arguments:

1) x

The x argument is a number that is divided by the second one.

2) y

The y argument is the divisor.

y must not be zero (0), otherwise, the function will issue the division by zero error.

Return Value

The MOD() function returns a number whose data type is the same as the input argument.

Examples

The following example shows how to use the MOD() function to get the remainder of two integers:

SELECT  MOD(15,4) modulus

The result is:

3

The following statement also returns the same result:

SELECT MOD(15,-4);

See the following statement:

SELECT MOD(-15,4);

The remainder is a negative number:

-3

Similarly, the following statement returns the same negative remainder number:

SELECT MOD(-15,-4);

The result is:

-3

In this tutorial, you have learned how to use the PostgreSQL MOD() function to find the remainder after the division of one number by another.

See more

PostgreSQL Tutorial: Math Functions

PostgreSQL Documentation: Mathematical Functions and Operators