In this tutorial, we will learn about the MySQL `LN()`

function. While studying maths, you must have come across the operation – log_{e }(logarithm to the base of the mathematical constant *e*). Finding the log_{e} of a number is also known as finding the natural logarithm of a number.

Logarithm, in general, is a widely used operation across many fields, and therefore, MySQL provides us with the `LN()`

function.

The `LN()`

function in MySQL is a mathematical function which is used to find the natural logarithm of a number. The `LN()`

function is the inverse of the `EXP()`

function. Let us explore the `LN()`

function now.

## Syntax of MySQL LN() function

`LN(number);`

Code language: SQL (Structured Query Language) (sql)

Where ‘number’ is a number greater than 0 whose natural logarithm is to be found out.

## Examples of MySQL LN() function

Let us start with some basic examples. How about finding the values of log_{e} 2, log_{e} 10 and log_{e }32 using the `LN()`

function. The queries for these operations are –

```
SELECT LN(2);
SELECT LN(10);
SELECT LN(32);
```

Code language: SQL (Structured Query Language) (sql)

And we get the output as,

### MySQL LN() With Zero and NULL

I mentioned earlier that the ‘number’ argument passed to the `LN()`

function should be greater than 0. So what if you pass zero or NULL to the `LN()`

function? In such a case, the `LN()`

function will return NULL. Let us demonstrate this using the below queries.

```
SELECT LN(0);
SELECT LN(NULL);
```

Code language: SQL (Structured Query Language) (sql)

And the output is,

### MySQL LN() With Negative Numbers

As you saw in the previous example, `LN()`

returns NULL if the argument is 0 or NULL. `MySQL LN()`

shows the same behavior when we pass a negative number as its parameter. Let us see this behavior using the below examples.

```
SELECT LN(-5);
SELECT LN(-2);
```

Code language: SQL (Structured Query Language) (sql)

And we get the output as,

### LN() With Expressions

Let us now use expressions with `MySQL LN()`

. Consider the below query.

`SELECT LN(3+3), LN(6);`

Code language: SQL (Structured Query Language) (sql)

Both expressions will output the same result as shown below.

Let us now find the result of the following expression using the LN() function – log_{e} 2^{3}. The query is,

`SELECT LN(POW(2, 3));`

Code language: SQL (Structured Query Language) (sql)

And the output is,

### How are LN() and EXP() related?

`LN()`

is the inverse of the `EXP()`

function. Let us see this using the below example. How about finding the natural logarithm of *e*? We do so using the below query.

`SELECT LN(EXP(1)) AS 'ln(e)';`

Code language: SQL (Structured Query Language) (sql)

And the output is,

As you can see, the output is 1, which is the number we passed as the argument. This shows that `LN()`

is the inverse of `EXP()`

. Let’s see a couple more examples before we move on to the next topic. Consider the following expressions – log_{e} *e*^{2} and log_{e} *e*^{55}. The queries are –

```
SELECT LN(EXP(2));
SELECT LN(EXP(55));
```

Code language: SQL (Structured Query Language) (sql)

And the output is,

## Using LN() With Tables

Consider the below Numbers table.

### Simple Example

Let’s kick things off with a simple example by finding the natural logarithm of all values in the X column. The query is,

`SELECT LN(X) FROM Numbers;`

Code language: SQL (Structured Query Language) (sql)

And we get the output as,

### LN() With the UPDATE statement

With reference to the previous example, let us create a new column named `LN_Of_X`

in the ‘Numbers’ table which contains the natural logarithm values of the values in the `X`

column. The queries for this are –

```
ALTER TABLE Numbers ADD LN_Of_X float;
UPDATE Numbers SET LN_Of_X=LN(X);
SELECT * FROM Numbers;
```

Code language: SQL (Structured Query Language) (sql)

We use the `ALTER`

statement to add the `LN_Of_X`

column of data type float to the Numbers table. Next, we update the pre-populated NULL values of the `LN_Of_X`

column with the natural logarithm values of the X column using the `UPDATE`

statement. Lastly, we display the newly updated Numbers table using the `SELECT`

statement.

The output is –

## Conclusion

Finding the natural logarithm of a number is one of the most important and widely used mathematical operations. I would encourage you to try the various use cases of `LN()`

in MySQL for practice.

## References

- MySQL Official Documentation on the
`LN()`

function.