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_IDProduct_NameProduct_QuantityPurchasing_PriceSelling_PriceRelease_DateProduct_Rating
104P110945NULL2022-04-30NULL
202P41545752022-01-285
103P21825NULL2022-02-184
111P7255152021-12-259
210P61550702021-10-15NULL
212P8191102502022-01-284
112P10105508352022-04-11NULL

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_IDSquare_of_ProductID_
10410816.0
20240804.0
10310609.0
11112321.0
21044100.0
21244944.0
11212544.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_PriceProduct_QuantitySquare_oftotalpriceSelling_PriceProduct_QuantitySquare_oftotalprice
9451089302500.0NULL10-
4515455625.075151265625.0
2518202500.0NULL18-
52515625.01525140625.0
5015562500.07015140625.0
11019562500.0250191102500.0
5501030250000.08351069722500.0

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

Output:

Product_RatingSquare_ofrating
NULL-
525.0
416.0
981.0
NULL-
416
NULL-





Latest Courses