MySQL LOG Function

MySQL LOG function is one of the Mathematical Function, which helps return the natural logarithmic value of a given number. If we specify the base value, then it returns the log value with base.

In this section, we show how to find natural logarithmic values in MySQL using Command Prompt and Workbench with natural examples.

MySQL LOG Syntax

The basic syntax of the LOG in MySQL is as shown below:

SELECT LOG(X) FROM Source

SELET LOG(Base, X)

If you use this LOG function with a single argument, it returns the natural logarithmic value of X. And, if we use two arguments, the base log value will return.

MySQL LOG Function Example 1

The LOG function returns the natural logarithmic value. The following query will show multiple ways to use LOG function.

SELECT LOG(1);

SELECT LOG(100);

SELECT LOG(35000);
MySQL LOG Function 1

TIP: LOG(2, X) is same as LOG2(X) function and LOG(10, X) is same as LOG10(X) function.

Here, we are using the base argument of LOG Function. The first MySQL statement returns the base 2 logarithmic value of 10. Within the third statement, LOG(10, 1000) means base 10 Logarithmic value of 10000.

SELECT LOG(2,10);

SELECT LOG(2,1000);

SELECT LOG(2,10000), LOG(10, 10000), LOG(1, 10000);
MySQL LOG Function 2

MySQL LOG Example 2

The MySQL LOG Function also allows you to find the natural logarithmic value of a column data. In this Mathematical Function example, we are going to find the natural logarithmic value, base 2 logarithmic value and base 10 logarithmic values for all the records present in the Standard Cost column.

SELECT EnglishProductName,
      Color,
      StandardCost,
      LOG(StandardCost) AS LogStdCost,
      LOG(2, StandardCost) AS Log2StdCost,
      LOG(10, StandardCost) AS Log10StdCost
 FROM `MySQL Tutorial`.`mathemetical functions`;
MySQL LOG Function 3