MySQL LOG() Function

The LOG() function in MySQL calculates logarithms. You can calculate the natural logarithm (base *e*) or a logarithm with a specified base.



LOG(): Definition and Usage

Logarithms are the inverse of exponential functions. The LOG() function in MySQL provides a way to compute logarithms. If you provide only one argument, it calculates the natural logarithm (base *e*, approximately 2.71828). If you provide two arguments, the second argument specifies the base of the logarithm.

Syntax

Syntax

LOG(number) -- Natural logarithm (base e)
LOG(base, number) -- Logarithm to a specified base
      

Parameter Values

Parameter Description
number The number for which you want to calculate the logarithm. This must be greater than 0. This is required.
base (Optional) The base of the logarithm (only used in the two-argument form). Must be greater than 1.

Related Functions

For other logarithmic functions, see LN() (natural logarithm) and EXP() (exponential function).

Examples

Natural Logarithm (Base e)

This example calculates the natural logarithm (base *e*) of 2.

Syntax

SELECT LOG(2);
      
Output

0.693147180559945
      

Logarithm with a Specified Base

This example calculates the logarithm of 4 with base 2 (which should be 2 because 2 to the power of 2 is 4).

Syntax

SELECT LOG(2, 4);
      
Output

2
      

**Note:** The output values are approximate due to floating-point limitations. The exact values may vary very slightly depending on your MySQL version. If you provide a number less than or equal to 0 for the `number` parameter, or a number less than or equal to 1 for the `base` parameter (when used), you'll get an error.