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.

jyoti rani

Recent Posts

What Is a Progressive Web App? Why Would You Need One?

App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…

1 year ago

7 Most Popular Paid Online Advertising Strategy

As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…

1 year ago

The Importance of User-Centered Design in Mobile App Development

Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…

1 year ago

Healthcare Mobile App Development: A Complete Guide for Founders

COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…

1 year ago

Exploring Diverse WordPress Theme Niches: A Comprehensive Guide

Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…

1 year ago

8 Awesome Blog Content Ideas for Movers to Skyrocket the SEO

For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…

1 year ago