DIV Function in SQL

The DIV is a string function in SQL which returns the quotient by dividing the first number from second number.

Syntax of DIV Function

In the DIV syntax, Number1 is the dividend and Number2 is the divisor.

In the Structured Query Language, we can also use the DIV 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 DIV function.

Examples of DIV function

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

Output:

Division_of_101by4
25

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

Output:

Division_of_2by2
1

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

Output:

Division_of_8by5
1

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

Output:

Division_of_255by200
1

Example 5: This example uses the DIV function with the SQL table.

In this example, we will create the new table through which we will perform the DIV 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 DIV function with the Product_Quantity column of the above Product_Details table:

This query divides each product_id by 100 and returns the quotient after division.

Output:

Product_ID Division_of_ProductID_by100
104 1
202 2
103 1
111 1
210 2
212 2
112 1

Query 2: The following SELECT query uses the DIV 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 quotient.

Output:

Purchasing_Price Product_Quantity Division_ofpurhcaseprice Selling_Price Product_Quantity Division_ofsellingprice
945 10 94 NULL 10 -
45 15 3 75 15 5
25 18 1 NULL 18 -
5 25 0 15 25 0
50 15 3 70 15 4
110 19 5 250 19 13
550 10 55 835 10 83

Query 3: The following SELECT query uses the DIV function with the Product_Rating column of the above Product_Details table:

This query divides each rating of product by 2 and returns the quotient after division.

Output:

Product_Rating Division_ofratingby2
NULL -
5 2
4 2
9 4
NULL -
4 2
NULL -