The POWER() function in PostgreSQL raises a number to the power of another number (exponentiation). It's essential for mathematical calculations, scientific formulas, exponential growth models, and statistical analyses.
Syntax
POWER(base, exponent)
-- Returns base raised to the power of exponent
-- Alternative operator syntax
base ^ exponent
Example usage
-- Basic exponentiation
SELECT POWER(2, 3) AS result;
-- Result: 8 (2^3 = 2 * 2 * 2)
SELECT POWER(10, 2) AS result;
-- Result: 100
-- Using ^ operator (equivalent)
SELECT 2 ^ 3 AS result;
-- Result: 8
-- Calculate square
SELECT POWER(5, 2) AS square;
-- Result: 25
-- Calculate cube
SELECT POWER(3, 3) AS cube;
-- Result: 27
-- Calculate square root (exponent = 0.5)
SELECT POWER(16, 0.5) AS square_root;
-- Result: 4
-- Calculate cube root (exponent = 1/3)
SELECT POWER(27, 1.0/3.0) AS cube_root;
-- Result: 3
-- Compound interest calculation
SELECT
principal_amount,
interest_rate,
years,
principal_amount * POWER(1 + interest_rate, years) AS future_value
FROM investments;
-- Exponential growth
SELECT
initial_population,
growth_rate,
time_periods,
initial_population * POWER(1 + growth_rate, time_periods) AS final_population
FROM population_data;
-- Distance calculation (Euclidean distance)
SELECT
SQRT(POWER(x2 - x1, 2) + POWER(y2 - y1, 2)) AS distance
FROM points;
-- Scientific notation conversion
SELECT
value,
coefficient * POWER(10, exponent) AS actual_value
FROM scientific_numbers;
-- Calculate variance manually
SELECT
AVG(POWER(value - avg_value, 2)) AS variance
FROM (
SELECT value, AVG(value) OVER () AS avg_value
FROM measurements
) t;
-- Negative exponents (reciprocal)
SELECT POWER(2, -3) AS result;
-- Result: 0.125 (1/8)
-- Fractional bases
SELECT POWER(0.5, 3) AS result;
-- Result: 0.125
-- Percentage calculations
SELECT
current_value,
previous_value,
((current_value / previous_value) - 1) * 100 AS percent_change,
POWER(current_value / previous_value, 1.0/years) - 1 AS annualized_growth_rate
FROM financial_data;
-- Area and volume calculations
SELECT
radius,
PI() * POWER(radius, 2) AS circle_area,
(4.0/3.0) * PI() * POWER(radius, 3) AS sphere_volume
FROM circles;
Common issues
- Integer division: Use NUMERIC for fractional exponents:
POWER(8, 1.0/3.0)notPOWER(8, 1/3). - Large exponents: Can result in overflow or very large numbers.
- Negative base with fractional exponent: May produce complex numbers (error in PostgreSQL).
- Zero to negative power: Results in division by zero error.
Best practices
- For square roots, SQRT() is more readable than POWER(x, 0.5).
- Cast to NUMERIC for precise fractional exponents:
POWER(base::NUMERIC, 1.0/3.0). - For large calculations, consider logarithms to avoid overflow:
EXP(exponent * LN(base)). - Use ^ operator for cleaner code when appropriate:
radius ^ 2instead ofPOWER(radius, 2).
Frequently Asked Questions
Q: What's the difference between POWER and the ^ operator?
A: They're functionally equivalent. POWER(2, 3) and 2 ^ 3 both return 8. The ^ operator is more concise but POWER is more portable across databases.
Q: How do I calculate square root using POWER?
A: Use an exponent of 0.5: POWER(16, 0.5) = 4. However, the SQRT() function is more readable: SQRT(16).
Q: Can I use negative exponents?
A: Yes, negative exponents return the reciprocal: POWER(2, -3) = 0.125 (which is 1/8).
Q: Why does POWER(8, 1/3) return 1 instead of 2?
A: Integer division: 1/3 = 0 in integer math. Use POWER(8, 1.0/3.0) or POWER(8::NUMERIC, 1.0/3.0) for fractional exponents.
Q: What happens with POWER(0, 0)?
A: In PostgreSQL, POWER(0, 0) returns 1, following mathematical convention, though it's technically undefined.