Javatpoint Logo
Javatpoint Logo

MOD Function in SQL

The MOD is a string function in SQL which returns the remainder from the division of first number by second number.

Syntax of MOD Function

In 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 function

Example 1: This example gets the remainder by dividing 101 by 4:

Output:

Division_of_101by4
1

Example 2: This example divides 101 by 4 and returns the remainder in result:

Output:

Division_of_2by2
0

Example 3: This example divides 8 by 5 and returns the remainder in result:

Output:

Division_of_8by5
3

Example 4: This example divides 255 by 200 and returns the remainder in result:

Output:

Division_of_255by200
55

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:


Product_ID Product_Name Product_Quantity Purchasing_Price Selling_Price Release_Date Product_Rating
104 P1 10 945 NULL 2022-04-30 NULL
202 P4 15 45 75 2022-01-28 5
103 P2 18 25 NULL 2022-02-18 4
111 P7 25 5 15 2021-12-25 9
210 P6 15 50 70 2021-10-15 NULL
212 P8 19 110 250 2022-01-28 4
112 P10 10 550 835 2022-04-11 NULL

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:

Product_ID Division_of_ProductID_by100
104 4
202 2
103 3
111 11
210 10
212 12
112 12

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:

Purchasing_Price Product_Quantity Division_ofpurhcaseprice Selling_Price Product_Quantity Division_ofsellingprice
945 10 5 NULL 10 -
45 15 0 75 15 0
25 18 7 NULL 18 -
5 25 5 15 25 15
50 15 5 70 15 10
110 19 15 250 19 3
550 10 0 835 10 5

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:

Product_Rating Division_ofratingby2
NULL -
5 1
4 0
9 1
NULL -
4 0
NULL -






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA