Javatpoint Logo
Javatpoint Logo

SQUARE Function in SQL

The SQUARE is a mathematical function in Structured Query Language which returns the square of any specified number. This function shows the result in floating value. We can specify both positive or negative number in the SQUARE function, but it always returns positive value.

Syntax of SQUARE String Function

Syntax1: This syntax uses the SQUARE function with the column name of the SQL table:

In the syntax, we have to specify the name of that column on which we want to perform the SQUARE string function. We can also use multiple square functions in single query.

Syntax2: This syntax uses the SQUARE function with the number:

Examples of SQUARE String function

Example 1: The following SELECT query shows the square of 4:

Output:

SQUARE_of_4
16.0

Example 2: following SELECT query shows the square of -20:

Output:

SQUARE_of_-20
400.0

Example 3: The following SELECT query shows the square of 6:

Output:

SQUARE_6.2
38.44

Example 4: The following SELECT query shows the 5 characters from the SQUARE of the given string

Output:

25

Example 5: This example uses the SQUARE function with the table in Structured Query Language.

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

This query shows the sqaure of each product Id in the resultant table

Output:

Product_ID Square_of_ProductID_
104 10816.0
202 40804.0
103 10609.0
111 12321.0
210 44100.0
212 44944.0
112 12544.0

Query 2:: The following SELECT query uses the SQUARE function with the multiplication of product quantity with Purchasing_Price and Selling_Price column of the above Product_Details table:

Output:

Purchasing_Price Product_Quantity Square_oftotalprice Selling_Price Product_Quantity Square_oftotalprice
945 10 89302500.0 NULL 10 -
45 15 455625.0 75 15 1265625.0
25 18 202500.0 NULL 18 -
5 25 15625.0 15 25 140625.0
50 15 562500.0 70 15 140625.0
110 19 562500.0 250 19 1102500.0
550 10 30250000.0 835 10 69722500.0

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

Output:

Product_Rating Square_ofrating
NULL -
5 25.0
4 16.0
9 81.0
NULL -
4 16
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