Sqrt Function In Sql in SQL
The SQRT function in SQL returns the square root of a given numeric expression.
? Syntax
SQRT(numeric_expression)numeric_expression: A non-negative number (integer or float) whose square root you want to compute.
? Return Type
Typically returns a float or double depending on the SQL dialect and input type.
? Basic Example
SELECT SQRT(25) AS SquareRoot; -- Returns 5.0?? Important Notes
| Condition | Behavior |
|---|---|
| Positive number | Returns square root |
| Zero | Returns 0 |
| Negative number | Returns NULL or error* |
*In many SQL engines (like SQL Server, MySQL), passing a negative number will return NULL or cause a runtime error, depending on the context.
? Use Cases
Mathematical or statistical calculations.
Distance or geometric formulas, e.g., Pythagorean theorem:
SELECT SQRT(POWER(x2 - x1, 2) + POWER(y2 - y1, 2)) AS Distance;Financial formulas, such as standard deviation.
? Advanced Example
SELECT Number, SQRT(Number) AS SquareRootFROM ( VALUES (0), (1), (4), (9), (16), (25)) AS Numbers(Number);This would return the square roots of those numbers in a result set.
Let me know if you want to use SQRT in a specific formula or dataset!