Skip to main content

MOD

The SQL MOD() function is a mathematical function that returns the remainder of the values from the division of two numbers. It calculates the modulo operation. This function is very useful when you want to find the remainder value after one number is divided by another.

Syntax

The syntax of the MOD function in SQL is:

MOD(expression1, expression2)
  • Expression1 and Expression2 are the values that you want to apply the function to.

Basic Usage

For instance, if you want to find the remainder of the division of 15 by 4 you would write:

SELECT MOD(15, 4) as result;

The result would be 3 because 3 is the remainder after dividing 15 by 4.

Usage with Table Columns

The MOD() function can also be applied to table columns. Let's imagine that you have a table named "Orders" with an "OrderNumber" column and you want to find the remainder of every order number when divided by 7, you would do:

SELECT OrderNumber, MOD(OrderNumber, 7) as result
FROM Orders;

This will return a list of all order numbers, along with the remainder when each order number is divided by 7.

Keep in mind that the SQL MOD() function may not work in the same way, or might not support all features, in every SQL database. Always refer to the documentation specific to the SQL database you are using.