The MOD function in Oracle SQL is a mathematical function that returns the remainder of one number divided by another. It's also known as the "modulus" function.
This function is extremely useful for tasks like determining if a number is odd or even, or for calculations that need to "wrap around."
What is the MOD Function in Oracle?
The MOD(n2, n1) function answers the question: "What is left over after n2 is divided by n1?"
MOD(10, 3)returns1(because 10 divided by 3 is 3 with a remainder of 1).MOD(10, 2)returns0(because 10 divided by 2 is 5 with a remainder of 0).MOD(5, 10)returns5(because 10 goes into 5 zero times, with a remainder of 5).
MOD Function Syntax
The syntax for MOD requires two arguments:
MOD(n2, n1)
Let's break that down:
n2(the dividend): The number to be divided.n1(the divisor): The number to divide by.
Oracle MOD Function Examples
Here are two practical examples of how to use MOD.
Example 1: Finding a Simple Remainder using MOD
This example calculates the remainder of 11 divided by 4.
Query:
SELECT
MOD(11, 4) AS "Remainder"
FROM DUAL;
Result:
Remainder
----------
3
(11 divided by 4 is 2, with 3 left over.)
Example 2: Checking for an Even or Odd Number with MOD
A very common use of MOD is to check if a number is even or odd. Any number that has a remainder of 0 when divided by 2 is even.
Query:
SELECT
MOD(17, 2) AS "Odd_Check",
MOD(18, 2) AS "Even_Check"
FROM DUAL;
Result:
Odd_Check Even_Check
---------- ----------
1 0
This result shows 17 is odd (remainder 1) and 18 is even (remainder 0).
