Understanding the POW() Function in SQLite

In SQLite, the pow() function calculates the result of raising one number to the power of another. In other words, it performs an exponentiation operation.

The pow() function is particularly useful for mathematical operations where powers or exponents are required.

It’s also available as power(). Both syntaxes do the same thing.

Syntax

pow(base, exponent)
-- or
power(base, exponent)
  • base: The base number you want to raise.
  • exponent: The power to which the base is raised.

Example 1

Let’s say you want to calculate 53 (5 raised to the power of 3). You can use the pow() function as follows:

SELECT pow(5, 3);

Result:

+-----------+
| pow(5, 3) |
+-----------+
| 125.0 |
+-----------+

In this example, 5 is raised to the power of 3, resulting in 125. In other words, 5 x 5 x 5 = 125.

Example 2

Here are some more examples, including numbers with decimal places, negative values, and zero:

SELECT 
       pow(2.5, 4),
       pow(-2.5, 4),
       pow(2.5, -4),
       pow(0, 4),
       pow(2.5, 0);

Result:

+-------------+--------------+--------------+-----------+-------------+
| pow(2.5, 4) | pow(-2.5, 4) | pow(2.5, -4) | pow(0, 4) | pow(2.5, 0) |
+-------------+--------------+--------------+-----------+-------------+
| 39.0625 | 39.0625 | 0.0256 | 0.0 | 1.0 |
+-------------+--------------+--------------+-----------+-------------+

The pow() function is useful in SQLite for any calculations involving exponents, and it supports both integer and floating-point inputs for flexibility in mathematical expressions.

Using a Negative Base

Using a negative base with an integer exponent works fine, but using a negative base with a fractional exponent returns NULL:

SELECT 
       power(-4, 2),
       power(-4, 0.5);

Result:

+--------------+----------------+
| power(-4, 2) | power(-4, 0.5) |
+--------------+----------------+
| 16.0 | null |
+--------------+----------------+

Important Points

  • Both parameters must be numeric
  • If either parameter is NULL, the result will be NULL
  • The function returns a floating-point number
  • Negative exponents are supported
  • If you need integer results, you may want to use round() or cast()
  • You can alternatively use power() to get the same results as pow()