SQRT Function in SQL

The SQRT is a SQL function of mathematics which gives the square root of the given number. Suppose, the number is 25, then this function returns 5.

Syntax of SQRT Function

In the SQRT syntax, we have to pass that number whose square root we want to find.

In the Structured Query Language, we can also use the SQRT function with the fields of the table as shown in the following block:

In this syntax, we have to define the name and column of that table on which we want to perform the SQRT function.

Examples of SQRT function

Example 1: This example returns the square root of the specified number:

Output:

Squareroot_of_100
10

Example 2: This example returns the square root of the specified number:

Output:

Squareroot_of_2
1.4142135623730

Example 3: This example returns the square root of 8:

Output:

Squareroot_of_8
2.8284

Example 4: This example returns the SQRTary representation of 255:

Output:

Squareroot_of_255
15.968719422671311

Example 5: This example returns the square root of theNULL value:

Output:

Squareroot_of_NULL
-

Example 6: This example returns the square root of numbers from 21 to 30:

Output:

21222324252627282930
458254.69044.79584.898955.09905.19655.29155.38515.4772

Example 7: This example uses the SQRT function with the SQL table.
In this example, we will create the new table through which we will perform the SQRT 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
104P110.250945NULL2022-04-30NULL
202P415.50045752022-01-285
103P218.25025NULL2022-02-184
111P725.2505152021-12-259
210P615.50050702021-10-15NULL
212P819.7501102502022-01-284
112P1010.2505508352022-04-11NULL

Query 1: The following SELECT query uses the SQRT function with the Product_Quantity column of the above Product_Details table:

This query shows the Square root of product id of each product.

Output:

Product_IDSquareroot_of_Product_ID
10410.1980
20214.2126
10310.1488
11110.53565
21014.4913
21214.56021
11210.58300

Query 2: The following SELECT query uses the SQRT function with the and Purchasing_Price and Selling_Price column of the above Product_Details table:

This query shows the square root of the purchasing and selling price of each product.

Output:

Purchasing_PriceSquareroot_of_PurchasingPriceSelling_PriceSquareroot_of_SellingPrice
94530.7048NULL-
456.70820758.66025
255NULL-
52.23606153.872983
507.07106708.36660026
11010.4880825015.81138
55023.452083528.8963

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

This query shows the square root of rating of each product in the above table.

Output:

Product_RatingSquareroot_of_productrating
NULL-
52.23606
42
93
NULL-
42
NULL-





Latest Courses