MOD

MOD is a mathematical function in SQL that returns the remainder of a division operation between two numbers. It's frequently used when there's a need to determine the remainder of a numeric division query.

MOD(N,M)

  • n: This indicates the dividend. It is the number that is to be divided.
  • m: This refers to the divisor. It is the number by which the dividend (N) is to be divided.

Example

SELECT MOD(10, 3);

Output

1

Explanation

The MOD function returns the remainder of a division. In this example, the division of 10 by 3 leaves a remainder of 1, which is the output.

MOD(Y, X)

  • y: The dividend in the modulo operation. This is the number that is to be divided.
  • x: The divisor in the modulo operation. This is the number by which the dividend will be divided. The result of the MOD(Y, X) function is the remainder of this operation.

Example

SELECT MOD(10, 3);

Output

1

Explanation

The MOD() function in PostgreSQL returns the remainder of the division of the first number by the second number. In this example, 10 divided by 3 equals 3 remainder 1, so MOD(10,3) returns 1.

MOD(dividend INT, divisor INT) RETURNS INT;

  • dividend int: This parameter represents the number to be divided. It must be an integer value.
  • divisor int: This parameter specifies the number by which the dividend will be divided. It should also be an integer. The function will return the remainder of this division operation.

Example

SELECT MOD(10,3) AS Result;

Output

Result
-----------
1

Explanation

In the SQL statement provided, the MOD function returns the remainder of the division of 10 by 3 which equals 1.

MOD(N, M)

  • n: This refers to the dividend. It is the numeric value to be divided in the MOD operation.
  • m: This refers to the divisor. It is the numeric value by which the dividend is divided in the MOD operation.

Example

SELECT MOD(8, 3)
FROM dual;

Output

2

Explanation

In the provided SQL statement, the MOD function returns the remainder of a division. In this case, dividing 8 by 3 leaves a remainder of 2. Oracle’s dual table is a one-row, one-column table present by default in all Oracle databases, which is used when there is a need for a select statement without an actual table.

MOD(X,Y)

  • x: The first numeric value that you wish to apply the modulo operation to. This can be any real number.
  • y: The second numeric value to use in the modulo operation. This is the value that ‘X’ will be divided by to derive the remainder. This number should not be zero as it would result in a division error.

Example

SELECT MOD(11, 3);

Output

2

Explanation

The MOD function returns the remainder of the division of the first number by the second. In this case, dividing 11 by 3 gives a quotient of 3 and a remainder of 2. Therefore, MOD(11, 3) returns 2.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.