PostgreSQL POWER() Function
Summary: in this tutorial, you will learn how to use the PostgreSQL POWER()
function to raise a number to a specific power.
Introduction to PostgreSQL POWER() function
The POWER()
function allows you to raise a number to a specific power. Here’s the basic syntax of the POWER()
function:
In this syntax:
base
: This is the base numberexponent
: This is the exponent to which you want to raise the base number.
The POWER()
function returns a numeric value representing the result of raising the base number to a specified exponent. It returns NULL
if either base
or exponent
is NULL
.
PostgreSQL POWER() function examples
Let’s take some examples of using the POWER()
function.
1) Basic POWER() function examples
The following example uses the POWER()
function to raise the number to the power of 3:
Output:
Similarly, you can use the POWER()
function with decimal values:
Output:
2) Using the POWER() function with negative exponent
The following example uses the POWER()
function with a negative exponent:
Output:
In this example, we raise 10 to the power of -2 resulting in 0.01
.
3) Using the POWER() function with a fractional exponent
The following example uses the POWER()
function to raise the number 2 to the power of 1.5
:
Output:
4) Using the POWER() function with table data
We’ll use the POWER()
function to calculate compound interest.
First, create a table called investments
to store the investment data:
Second, insert some rows into the investments
table:
Output:
Third, calculate the compound interest of each investment in the investments
table:
Output:
The query returns the initial investment details along with the calculated future value for each investment.
To calculate the compound interest of each investment:
- Calculate the interest rate using the
POWER()
function. - Use the
ROUND()
function to round the future value to two decimal places.
Summary
- Use the PostgreSQL
POWER()
function to raise a number to a specific power.