In MySQL, the POW()
function raises a number to the power of another number.
You provide the two values as arguments when calling the function.
Syntax
This syntax goes like this:
POW(X,Y)
The function returns the value of X
raised to the power of Y
.
Alternatively, you can use the following syntax:
POWER(X,Y)
This is because POWER()
is a synonym for POW()
.
Example 1 – Basic Usage
Here’s a basic example to demonstrate how this function works.
SELECT POW(5, 2);
Result:
+-----------+ | POW(5, 2) | +-----------+ | 25 | +-----------+
So that example is like doing this:
SELECT 5 * 5;
Result:
+-------+ | 5 * 5 | +-------+ | 25 | +-------+
And if we do this:
SELECT POW(5, 3);
Result:
+-----------+ | POW(5, 3) | +-----------+ | 125 | +-----------+
It’s like doing this:
SELECT 5 * 5 * 5;
Result:
+-----------+ | 5 * 5 * 5 | +-----------+ | 125 | +-----------+
And so on.
Example 2 – Negative Base Number
You can also use negative values for the base number.
If you raise a negative number to the power of an even number, the result is a positive number:
SELECT POW(-5, 2);
Result:
+------------+ | POW(-5, 2) | +------------+ | 25 | +------------+
However, if you raise a negative number to the power of an odd number, the result is negative:
SELECT POW(-5, 3);
Result:
+------------+ | POW(-5, 3) | +------------+ | -125 | +------------+
Example 3 – Negative Exponent
You can also use negative exponent values.
Here are a couple examples using a negative exponent with a positive base number.
SELECT POW(5, -2), POW(5, -3);
Result:
+------------+------------+ | POW(5, -2) | POW(5, -3) | +------------+------------+ | 0.04 | 0.008 | +------------+------------+
And the following two examples use a negative base number (and a negative exponent):
SELECT POW(-5, -2), POW(-5, -3);
Result:
+-------------+-------------+ | POW(-5, -2) | POW(-5, -3) | +-------------+-------------+ | 0.04 | -0.008 | +-------------+-------------+
The POWER() Function
The POWER()
function is a synonym for POW()
. Therefore, we can use it in place of any of the above examples. For example, we can rewrite the previous example to this:
SELECT POWER(-5, -2), POWER(-5, -3);
Result:
+---------------+---------------+ | POWER(-5, -2) | POWER(-5, -3) | +---------------+---------------+ | 0.04 | -0.008 | +---------------+---------------+
And we get the same result.