How to Use MOD() Function in PostgreSQL?

PostgreSQL offers several math functions such as ABS(), MOD(), ROUND(), etc. All these functions assist the users in performing mathematical operations conveniently and efficiently. If we talk about the MOD() function, it is one of the most frequently used mathematical functions that perform the modulo operation on two numbers and retrieves the remainder.

This post will teach you how to use MOD() Function in PostgreSQL using practical examples. So, let’s start.

How to Use MOD() Function in PostgreSQL?

In Postgres, the MOD() function accepts two numbers as arguments, performs division on the given numbers, and retrieves the remainder.

Syntax:

The below snippet will assist you in understanding the syntax of the MOD() function:

MOD(num_1, num_2);

The syntax shows that the MOD() function takes two arguments: num_1 represents a dividend, and num_2 represents a divisor.

The MOD() function will perform the division on the given numbers and retrieves the remainder/quotient.

The divisor must be a non-zero; otherwise, the MOD() function will throw a “division by zero” error.

Example#1: Positive Integers

In this example, we will pass two positive integers to the MOD() function, i.e., 150 and 7:

SELECT MOD(150, 7);
img

The MOD() function retrieves the resultant remainder.

Example#2: Division BY Zero

Let’s pass the 0 as a divisor:

SELECT MOD(150, 0);
img

On passing 0 as a divisor, the MOD() function throws a "division by zero" error.

Example#3: Negative Divisor

In this example, we will pass a positive dividend and a negative divisor to the MOD() function:

SELECT MOD(170, -8);
img

The MOD() function retrieves the positive remainder.

Example#4: Negative Dividend

Let’s pass a negative dividend and positive divisor to the MOD() function:

SELECT MOD(-170, 8);
img

We can conclude from examples 3 and 4 that the dividend's sign determines the sign of the retrieved value.

Example#5: Positive Decimal Values

In this example, we will see how the MOD() function deals with the decimal values:

SELECT MOD(154.72, 12.14);
img

This is how the MOD() function works with decimal values.

Example#6: How to Use MOD() Function on Table’s Data?

Let’s create a table “modulus_example” with two columns: first_number, second_number:

CREATE TABLE modulus_example(
first_number INT,
second_number INT
);
img

The modulus_example table with two integer type columns has been created successfully. Let’s insert some integers into the newly created modulus_example table:

INSERT INTO modulus_example(first_number, second_number)
VALUES (120, 7),
(500, 8),
(639, 4),
(-72, 5),
(-344, 11);
img

The specified values have been inserted into the modulus_example table. Let’s use the MOD() function to perform the modulus operation on the table’s data:

SELECT first_number, second_number, 
MOD(first_number, second_number) AS remainder
FROM modulus_example;
img

This way, you can use the MOD() function on the table’s data to get the remainder values.

Conclusion

The MOD() function is one of the most frequently used mathematical functions in Postgres that perform the modulo operation on two numbers and retrieves the remainder. It accepts two numbers as arguments, performs division on the given numbers, and retrieves the remainder. This write-up considered multiple use cases to explain the working of the MOD() function.