Open In App

SQRT() Function in MySQL

Last Updated : 19 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

sqrt_result
4.0000

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:

numsqrt_value
93.0000
164.0000
255.0000
366.0000
497.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:

sqrt_decimal
4.5000

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.


Next Article
Article Tags :

Similar Reads