How to use LN() function in MySQL8 ?

With MySQL, we can do complex calculations very easily with the help of inbuilt mathematical functions. LN(X) function is used to get the natural logarithm of the number X.

Now let’s start with it.


LN(X) is a mathematical function. It returns the natural logarithm of a number X ; that means, the base-e logarithm of X. Where X is the value passed as an argument.

It will return NULL, if X is passed as NULL.

It will returns NULL and a warning “Invalid argument for logarithm” is reported,  If X is less than or equal to 0.0E0, the function

Note: This function is synonymous with LOG(X). The inverse of this function is EXP().

MySQL LN() : Syntax

LN ( X );

MySQL LN() : Parameter

Name, Required /Optional, Description
X , Required, Double, It represents a valid number for fetching log value.


MySQL LN() : Output

Return, Description
NULL, if the argument is NULL.
Double, It returns the natural logarithm of a number X .


MySQL LN()  Available from : MySQL 4.0


LN() Example 1 : Basic Examples

Now, Let’s see some of the basic examples of it and see what it returns.

mysql> SELECT LN(5);
+--------------------+
| LN(5) |
+--------------------+
| 1.6094379124341003 |
+--------------------+
1 row in set (0.01 sec)

mysql> SELECT LN(8.56);
+--------------------+
| LN(8.56) |
+--------------------+
| 2.1471001901536506 |
+--------------------+
1 row in set (0.00 sec)

mysql> SELECT LN(1);
+-------+
| LN(1) |
+-------+
| 0 |
+-------+
1 row in set (0.00 sec)

LN() Example 2 : Negative values

Now, Let’s see an example with a negative value and see what it returns.

mysql> SELECT LN(-5);
+--------+
| LN(-5) |
+--------+
| NULL |
+--------+
1 row in set, 1 warning (0.03 sec)

mysql> SHOW WARNINGS;
+---------+------+--------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------+
| Warning | 3020 | Invalid argument for logarithm |
+---------+------+--------------------------------+
1 row in set (0.00 sec)

mysql> SELECT LN(0);
+-------+
| LN(0) |
+-------+
| NULL |
+-------+
1 row in set, 1 warning (0.00 sec)

mysql> SHOW WARNINGS;
+---------+------+--------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------+
| Warning | 3020 | Invalid argument for logarithm |
+---------+------+--------------------------------+
1 row in set (0.00 sec)

We have got the NULL as output and a warning has been issued. Warning data can also be seen with the help of ‘SHOW WARNINGS;’ command. You can see the above code for the same.


LN() Example 3 : With Expressions

We are using the LN() function with expressions. See the below example.

mysql> SELECT LN(1+1);
+--------------------+
| LN(1+1) |
+--------------------+
| 0.6931471805599453 |
+--------------------+
1 row in set (0.00 sec)

mysql> SELECT LN(4.67+11.89);
+--------------------+
| LN(4.67+11.89) |
+--------------------+
| 2.8069901489571136 |
+--------------------+
1 row in set (0.00 sec)

mysql> SELECT LN(3*2);
+-------------------+
| LN(3*2) |
+-------------------+
| 1.791759469228055 |
+-------------------+
1 row in set (0.01 sec)

mysql> SELECT LN(3.8*2.2);
+--------------------+
| LN(3.8*2.2) |
+--------------------+
| 2.1234584270966104 |
+--------------------+
1 row in set (0.00 sec)

LN() Example 4 : NULL arguments

If the argument is NULL, it will return NULL. See the below example :

mysql> SELECT LN(NULL);
+----------+
| LN(NULL) |
+----------+
| NULL |
+----------+
1 row in set (0.00 sec)

Related articles : EXP(), MySQL Maths.

You may also like...