SQL - SQRT() Function
The SQL SQRT() function returns the square root of a positive number. This accept the non-negative number as a parameter. Suppose if the number is 25, then this function returns 5.
Suppose we have a number of columns in a table and we want to fetch the square root of a particular column that accepts only numeric values. So we cannot find the square root of every entry manually. Thus, we can use the SQRT() function and pass the column name into it to get the square root of every entry.
Syntax
Following is the syntax of the SQRT() function −
SELECT SQRT(X) AS Alias_Name;
where x is the parameter that should always be positive.
Following is the syntax to use the SQRT function in a SQL table −
SELECT SQRT(Integer_column_name) AS Alias_Name FROM table_name;
We can use the above syntax, which will accept the column name that accepts the positive values, to perform the SQRT function to find the square root value.
Example
In the following example, we are fetching the square root of the 144 using the SQRT() function.
Following is the query −
SELECT SQRT(144) AS squareRoot;
Output
We get the following output on the execution of the above SQL query −
+------------+ | squareRoot | +------------+ | 12 | +------------+
Example
In the following example, we are passing a number as a string to calculate the square root of the specified string using the SQRT() function.
Following is the query −
SELECT SQRT('400') AS squareRoot;
Output
On execution of the above SQL query, we get the square root of the given number as a string −
+------------+ | squareRoot | +------------+ | 20 | +------------+
Example
In the following example, we are using the negative value to fetch the square root of the negative value.
Following is the query −
SELECT SQRT(-400) AS squareRoot;
Verification
When we execute the above SQL query, we get the following error because the SQRT does not accept the negative value in the SQL. But it displays the NULL value in MySQL.
An invalid floating point operation occurred.
Example
In the following example, we are fetching the ID and name from the customer's table and calculating the square root of the salary.
Letâs create a table named customers using the CREATE statement −
CREATE TABLE customers(ID INT NOT NULL PRIMARY KEY(ID), NAME VARCHAR(30) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(30), SALARY DECIMAL(18, 2));
Letâs insert the data into the CUSTOMERS using the INSERT statement −
insert INTO customers VALUES(1, 'Ramesh', 32, 'Ahmedabad', 2000); insert INTO customers VALUES(2, 'Aman' 23, 'Ranchi', 40000); insert INTO customers VALUES(3, 'kaushik', 23, 'Kota', 2000); insert INTO customers VALUES(4, 'Chaitali', 25, 'Mumbai', 6500); insert INTO customers VALUES(5, 'Rakesh', 24, 'Kota', 30000); insert INTO customers VALUES(6, 'Vivek', 22, 'Ranchi', 35000); insert INTO customers VALUES(7, 'Akash', 22, 'Ranchi', 50000);
Following is the customers table −
+------+----------+------+-----------+--------+ | ID | NAME | AGE | ADDRESS | SALARY | +------+----------+------+-----------+--------+ | 1 | Ramesh | 32 | Ahmedabad | 2000 | | 2 | Aman | 23 | Ranchi | 40000 | | 3 | kaushik | 23 | Kota | 2000 | | 4 | chsitali | 25 | Mumbai | 6500 | | 5 | Rakesh | 24 | kota | 30000 | | 6 | Vivek | 22 | Ranchi | 35000 | | 7 | Akash | 22 | Ranchi | 50000 | +------+----------+------+-----------+--------+
Following is the SQL SQRT() function query −
SELECT ID, NAME, SQRT(SALARY) FROM customers;
Output
On execution of the above SQL query, we obtain the ID, NAME, and square root of the salary −
+------+----------+--------------------+ | ID | NAME | SQRT(SALARY) | +------+----------+--------------------+ | 1 | Ramesh | 44.721359549995796 | | 2 | Aman | 200 | | 3 | kaushik | 44.721359549995796 | | 4 | Chaitali | 80.62257748298549 | | 5 | Rakesh | 173.20508075688772 | | 6 | Vivek | 187.08286933869707 | | 7 | Akash | 223.60679774997897 | +------+----------+--------------------+