How LOG10() Works in MariaDB

In MariaDB, LOG10() is a built-in function that returns the base-10 logarithm of its argument.

Syntax

The syntax goes like this:

LOG10(X)

Where X is the value for which to return the base-10 logarithm.

Example

Here’s an example to demonstrate:

SELECT LOG10(3);

Result:

+---------------------+
| LOG10(3)            |
+---------------------+
| 0.47712125471966244 |
+---------------------+

Here are some more:

SELECT 
    LOG10(10),
    LOG10(100),
    LOG10(1000),
    LOG10(10000);

Result:

+-----------+------------+-------------+--------------+
| LOG10(10) | LOG10(100) | LOG10(1000) | LOG10(10000) |
+-----------+------------+-------------+--------------+
|         1 |          2 |           3 |            4 |
+-----------+------------+-------------+--------------+

Argument Ranges

If X is less than or equal to 0, then NULL is returned with a warning.

SELECT 
    LOG10(0),
    LOG10(-2);

Result:

+----------+-----------+
| LOG10(0) | LOG10(-2) |
+----------+-----------+
|     NULL |      NULL |
+----------+-----------+
1 row in set, 2 warnings (0.001 sec)

Let’s check the warnings:

SHOW WARNINGS;

Result:

+---------+------+---------------+
| Level   | Code | Message       |
+---------+------+---------------+
| Warning | 1365 | Division by 0 |
| Warning | 1365 | Division by 0 |
+---------+------+---------------+

Non-Numeric Arguments

Here’s an example of what happens when we provide non-numeric arguments:

SELECT LOG10('Homer');

Result:

+----------------+
| LOG10('Homer') |
+----------------+
|           NULL |
+----------------+
1 row in set, 2 warnings (0.000 sec)

Let’s see the warning:

SHOW WARNINGS;

Result:

+---------+------+-------------------------------------------+
| Level   | Code | Message                                   |
+---------+------+-------------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: 'Homer' |
| Warning | 1365 | Division by 0                             |
+---------+------+-------------------------------------------+

Null Arguments

LOG10() returns null if its argument is null:

SELECT LOG10(null);

Result:

+-------------+
| LOG10(null) |
+-------------+
|        NULL |
+-------------+

Missing Arguments

Calling LOG10() with the wrong number of arguments, or without any arguments results in an error:

SELECT LOG10();

Result:

ERROR 1582 (42000): Incorrect parameter count in the call to native function 'LOG10'

And:

SELECT LOG10(10, 2);

Result:

ERROR 1582 (42000): Incorrect parameter count in the call to native function 'LOG10'