MOD Function in SQLThe MOD is a string function in SQL which returns the remainder from the division of first number by second number. Syntax of MOD FunctionIn the MOD syntax, Number1 is the dividend and Number2 is the divisor. In the Structured Query Language, we can also use the MOD function with the columns of the table as shown in the following block: In this syntax, we have to define the name and columns of that table on which we want to perform the MOD function. Examples of MOD functionExample 1: This example gets the remainder by dividing 101 by 4: Output:
Example 2: This example divides 101 by 4 and returns the remainder in result: Output:
Example 3: This example divides 8 by 5 and returns the remainder in result: Output:
Example 4: This example divides 255 by 200 and returns the remainder in result: Output:
Example 5: This example uses the MOD function with the SQL table. In this example, we will create the new table through which we will perform the MOD function on the columns of the table: The following shows the syntax to create the new table in SQL: The following CREATE statement creates the Product_Details table for storing the price and quantity of products: The following multiple INSERT queries insert the records of products with their selling and purchasing price into the Product_Details table: The following SELECT statement displays the inserted records of the above Product_Details table:
Query 1: The following SELECT query uses the MOD function with the Product_Quantity column of the above Product_Details table: This query divides each product_id by 100 and returns the remainder after division. Output:
Query 2: The following SELECT query uses the MOD function with the and Purchasing_Price and Selling_Price column of the above Product_Details table: This query divides the purchasing price and selling price of each product by product quantity and returns the remainder. Output:
Query 3: The following SELECT query uses the MOD function with the Product_Rating column of the above Product_Details table: This query divides each rating of product by 2 and returns the remainder after division. Output:
Next TopicOCT Function in SQL |