The SQRT() function in MySQL calculates the square root of a non-negative number, returning NULL for negative inputs. It is a built-in function that provides high precision and is optimized for performance and making it ideal for mathematical and scientific applications.
In the article, we will cover the SQRT() Function in MySQL with its definition, syntax, and examples along with its advantages.
SQRT() Function in MySQL
The SQRT() function in MySQL is used to compute the square root of a given non-negative number. It returns the square root of the number as a floating-point value.
If the input is negative, the function returns NULL. The function is commonly used for mathematical calculations and is built into MySQL for convenience and performance.
Syntax:
SELECT SQRT(number);
Parameter:
- number: The value for which you want to calculate the square root. It must be a non-negative number; otherwise, MySQL will return NULL.
Creating the Database and Table
Let's create a simple database.
-- Create a database
CREATE DATABASE math_operations;
-- Select the database
USE math_operations;
-- Create a table to store numbers
CREATE TABLE numbers (
id INT AUTO_INCREMENT PRIMARY KEY,
num FLOAT
);
-- Insert some sample numbers
INSERT INTO numbers (num) VALUES (9), (16), (25), (36), (49);
Examples of SQRT() Function in MySQL
Example 1: Finding the Square Root of a Single Value
In this example, we will calculate the square root of the number 16.
SELECT SQRT(16) AS sqrt_result;
Output:
Explanation: This table displays a single column, sqrt_result
, with one row containing the value 4.0000
. This value represents the square root of a number, formatted to four decimal places.
Example 2: Using SQRT() with Data from a Table
Let's calculate the square roots of the numbers stored in the numbers table.
SELECT num, SQRT(num) AS sqrt_value
FROM numbers;
Output:
num | sqrt_value |
---|
9 | 3.0000 |
16 | 4.0000 |
25 | 5.0000 |
36 | 6.0000 |
49 | 7.0000 |
Explanation: This table shows a list of numbers (num
) and their corresponding square roots (sqrt_value
). Each row provides the square root value for a specific number, formatted to four decimal places. For example, the square root of 9
is 3.0000
, and the square root of 49
is 7.0000
.
Example 3: Square Root with a Non-Negative Decimal
The SQRT() function can also handle decimal numbers. Here’s an example:
SELECT SQRT(20.25) AS sqrt_decimal;
Output:
Explanation: This table displays a single column, sqrt_decimal
, with one row containing the value 4.5000
. This value represents the square root of a number, expressed in a decimal format with four decimal places.
Advantages of SQRT() Function
Here are some advantages of SQRT() Function:
- Built-in Functionality: The SQRT() function is built-in, making it easy to use without needing to implement custom logic for square root calculations.
- Precision: It calculates square roots with high precision, suitable for scientific and mathematical applications.
- Performance: Since it's a native MySQL function, SQRT() is optimized for performance, allowing quick calculations even when dealing with large datasets.
- Error Handling: It automatically returns NULL for invalid input (e.g., negative numbers), simplifying error management in queries.
Conclusion
The SQRT() function in MySQL is a simple yet powerful tool for finding square roots. It handles both whole numbers and decimals, providing results in an easy-to-use format. Whether you're working on everyday calculations or more complex mathematical queries, this function makes your work more efficient and accurate.
Similar Reads
STD() function in MySQL
With the help of STD() function we can calculate population Standard deviation of an expression in MySQL. But, if there are no matching rows in the given expression it returns Null. Syntax : STD(expr); Parameter : This method accepts only one parameter. expr : Input expression from which we want to
3 min read
RIGHT() Function in MySQL
RIGHT() function in MySQL is used to extract a specified number of characters from the right side of a given string. Second argument is used to decide, how many characters it should return. Syntax : RIGHT( str, len ) Parameter : This function accepts two parameter as mentioned above and described be
3 min read
RTRIM() Function in MySQL
RTRIM() : It is the function in MySQL that is used to remove trailing spaces from a string. Syntax : RTRIM(str) Parameter : RTRIM() function accepts one parameter as mentioned above and described below. str âThe string from which we want to remove trailing spaces. Returns : It returns a string after
3 min read
SUM() Function in MySQL
The SUM() function in MySQL is a powerful aggregate function used to calculate the total sum of values in a numeric column. By summing up the values in the specified column, this function helps in generating overall totals and performing calculations that provide meaningful insights from our data. I
4 min read
TAN() Function in MySQL
TAN() function : This function in MySQL is used to return the tangent of a specified number. In any right triangle, the tangent of an angle is the length of the opposite side divided by the length of the adjacent side. Similarly, this can also be defined as tangent of x is the sine of x divided by t
1 min read
OCT() function in MySQL
OCT() function in MySQL is used to convert decimal number to octal. It returns equivalent octal value of a decimal number. Syntax : OCT(number) Parameter : This method accepts only one parameter. number : The decimal number which we want to convert. Returns : It returns octal value of a decimal numb
2 min read
ORD() Function in MySQL
ORD() function in MySQL is used to find the code of the leftmost character in a string . If the leftmost character is not a multibyte character, it returns ASCII value. And if the leftmost character of the string str is a multibyte character, ORD returns the code for that character, calculated from
3 min read
STRCMP() Function in MySQL
STRCMP() function in MySQL is used to compare two strings. If both of the strings are same then it returns 0, if the first argument is smaller than the second according to the defined order it returns -1 and it returns 1 when the second one is smaller the first one. Syntax : STRCMP(Str1, Str2) Param
3 min read
STDDEV() function in MySQL
Sometimes we need to calculate population Standard deviation of an expression in MySQL. STDDEV() function can be used for this purpose in MySQL. It returns NULL if no matching rows found in the given expression. Syntax : STDDEV(expr); Parameter : This method accepts only one parameter. expr : Input
3 min read
TIME() Function in MySQL
The TIME() function in MySQL is used to extract the time portion from a date or datetime expression, returning the time in the format 'HH:MM'. This function is particularly useful when working with time components in databases, such as scheduling or logging systems. In this article, We will learn ab
4 min read