NumbersSQL standard

POWER

Raises a number to a power.

Syntax

POWER(<number>, <exponent>)
<number> ^ <exponent>

POWER(2, 10) gives 1024. The exponent can be fractional or negative: POWER(9, 0.5) is the same as a square root, and POWER(2, -1) is a half. For the root itself there's a shorter function, SQRT.

An integer, a fractional and a negative exponent
SELECT POWER(2, 10) AS power_of_two,
POWER(9, 0.5) AS square_root,
POWER(2, -1) AS half;
power_of_twosquare_roothalf
10243.00000000000000000.5
Compound interest
SELECT ROUND(1000 * POWER(1.05, 3), 2) AS after_3_years;
after_3_years
1157.63

1000 at 5% a year: every year the amount is multiplied by 1.05, so over three years — by 1.05 to the third power.

Bytes to megabytes
SELECT 5000000 / POWER(1024, 2) AS megabytes;
megabytes
4.76837158203125

A megabyte has 1024 · 1024 bytes — that is, 1024 to the second power.

Watch out

Integer arguments give double precision, and with it — rounding errors: POWER(1.1::float8, 2) gives 1.2100000000000002. With a decimal literal the calculation is done in numeric, and POWER(1.1, 2) gives exactly 1.21.

^ raises to a power too: 2 ^ 10. But 2 ** 10, as in some programming languages, throws operator does not exist.

A negative number raised to a fractional power is an error: POWER(-8, 1.0 / 3) says a negative number raised to a non-integer power yields a complex result.

Related functions