SQUARE Function in SQLThe 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 FunctionSyntax1: 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 functionExample 1: The following SELECT query shows the square of 4: Output:
Example 2: following SELECT query shows the square of -20: Output:
Example 3: The following SELECT query shows the square of 6: Output:
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:
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:
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:
Query 3: The following SELECT query uses the SQUARE function with the Product_Rating column of the above Product_Details table: Output:
Next TopicABS Function in SQL |