Using mathematical operations, we can use numerical data to create new datasets and insightful information because it is unavoidable when working with databases.
This short article will discuss how to use the mod function in Standard SQL.
What is the Mod function in SQL?
The mod() function is part of SQL mathematical functions. It is used to determine the remainder of a division operation.
In most programming languages, you will find this operator under the name, modulo operator.
The function takes two values and performs the division against them. It then returns the remainder from the division operation.
The function syntax is as shown:
In this case, x refers to the dividend while y represents the divider.
Let us take a look at these examples:
1. Example
Consider the example below that returns the remainder from dividing 233 by 34.
MOD(233, 34) AS remainder;
The above example should return output as shown below:
29
2. Example
Since the mod function returns the remainder from a division operation, attempting to divide a value by 0 will return an error.
An example is shown:
MOD(233, 0) AS remainder;
This should return an error as shown below:
3. Example
The mod function supports negative values. An example is shown below:
MOD(-233, 34) AS remainder;
This should return:
-29
4. Example
In Standard SQL, the mod function does not support floating-point values. However, this may be adopted differently by various database engines.
An example operation with a float will return an error.
MOD(23.5, 10.0) AS remainder;
This should return:
Conclusion
This article describes the use of the mod function in Standard SQL. This function allows you to determine the remainder of a division operation.