Logarithm in PostgreSQL – LOG(),LN(), LOG10() function

In PostgreSQL LOG(), LN(), and LOG10() function are ,most commonly used functions to compute logarithms , although there are several functions to compute logarithms. The most commonly used are LOG(), LN(), and LOG10() function. Each function has its specific use case:

  1. LOG(base, numeric): Computes the logarithm of a number to a specified base.
  2. LN(numeric): Computes the natural logarithm (base e) of a number.
  3. LOG10(numeric): Computes the base 10 logarithm of a number.

PostgreSQL provides several functions to compute logarithms, allowing you to work with different bases and perform various mathematical and data analysis tasks. The primary functions are LOG() for custom bases, LN() for the natural logarithm, and LOG10() for base 10 logarithms. These functions can be applied to both constant values and column values in a table.

 

LOG() Function in PostgreSQL Syntax:

LOG(base, numeric):

LOG() function computes the logarithm of a number to a specified base.

Example:

 

SELECT LOG(2, 16) AS log_base2_of_16, LOG(10, 100) AS log_base10_of_100;

Result:

Compute Logarithm in PostgreSQL – LOG() function 1

 

 

LN() Function in PostgreSQL – Natural Logarithm (base e):

Syntax:

LN(numeric)

LN() function computes the natural logarithm (base e) of a number.

 

Example:

 

SELECT LN(2.7182818284590453) AS natural_log_of_e, LN(10) AS natural_log_of_10;

Result:

Compute Logarithm in PostgreSQL – LOG() function 2

 

 

LOG10() Function in PostgreSQL:

Syntax:

LOG10(numeric):

LOG10() function computes the logarithm of a number to the base of 10

Example:

 

SELECT LOG10(100) AS log10_of_100, LOG10(1000) AS log10_of_1000;

Result:

Compute Logarithm in PostgreSQL – LOG() function 3

 

 

Get the Logarithm of column in PostgreSQL table:

We will be using measurements table.

Compute Logarithm in PostgreSQL – LOG() function 4

 

LOG() Function of column in PostgreSQL:

In our example we will use log() function to compute Logarithm to base 2 of a column in PostgreSQL table

 

SELECT id, value, LOG(2, value) AS log_base2 FROM measurements;

Using log() To compute logarithms to base 2 for the values:

Output:

Compute Logarithm in PostgreSQL – LOG() function 5

 

LN() Function of column in PostgreSQL – Natural logarithm (base e):

In our example we will use LN() function to compute Logarithm to base e of a column in PostgreSQL table

 

SELECT id, value, LN(value) AS natural_log FROM measurements;

Using LN() To compute natural logarithms to base e for the values:

Output:

Compute Logarithm in PostgreSQL – LOG() function 6

 

 

LOG10() Function of column in PostgreSQL:

In our example we will use log10() function to compute Logarithm to base 10 of a column in PostgreSQL table

 

SELECT id, value, LOG10(value) AS log10 FROM measurements;

Using log10() To compute logarithms to base 10 for the values:

Output:

Compute Logarithm in PostgreSQL – LOG() function 7

 

 

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts