PostgreSQL SQRT() Function
Summary: in this tutorial, you will learn how to use the PostgreSQL SQRT()
function to calculate the square root of a number.
Introduction to the PostgreSQL SQRT() function
The SQRT()
function is a powerful mathematical function that allows you to calculate the square root of a number.
Here’s the basic syntax of the SQRT()
function:
In this syntax, the number
is a numeric value for which you want to calculate the square root
The SQRT()
function returns the square root of the input number
.
PostgreSQL SQRT() function examples
Let’s take some examples of using the SQRT()
function.
1) Basic SQRT() function example
The following example uses the SQRT()
function to return the square root of 25:
Output:
The query returns the square root of 25, which is 5.
2) Using PostgreSQL SQRT() function to calculate distance
Suppose you have a table called coordinates
that consists of columns x
and y
representing the coordinates of points in two-dimensional space:
Output:
The following query uses the SQRT()
function to calculate the distance of each point from the origin (0,0):
Output:
Summary
- Use the PostgreSQL
SQRT()
function to calculate the square root of a number.