SQLite Tutorial SQLite Advanced SQLite Database SQLite References

SQLite LOG() Function



The SQLite LOG() function returns the base-10 logarithm or logarithm to the specified base of a given number. In special cases it returns the following:

  • If the number is less than or equal to 0, then NULL is returned.
  • If the base is less than or equal to 1, then NULL is returned.

Syntax

/* base-10 logarithm of number */
LOG(number)

/* logarithm of number to the specified base */
LOG(base, number)

Parameters

base Specify the number. Must be greater than 0 and not equal to 1.
number Required. Specify the number. Must be greater than 0.

Return Value

Returns the base-10 logarithm or logarithm to the specified base of a given number.

Example 1:

The example below shows the usage of LOG() function.

SELECT LOG(1);
Result: 0.0

SELECT LOG(1.5);
Result: 0.176091259055681

SELECT LOG(5);
Result: 0.698970004336019

SELECT LOG(0);
Result: NULL

SELECT LOG(3, 4);
Result: 1.26185950714291

SELECT LOG(0.7, 0.8);
Result: NULL

SELECT LOG(5, 10);
Result: 1.43067655807339

SELECT LOG(5, 25);
Result: 2.0

Example 2:

Consider a database table called Sample with the following records:

Datax
Data 10.5
Data 21
Data 35
Data 410
Data 550

The statement given below can be used to calculate the base-10 logarithm of column x.

SELECT *, LOG(x) AS LOG_Value FROM Sample;

This will produce the result as shown below:

DataxLOG_Value
Data 10.5-0.301029995663981
Data 210.0
Data 350.698970004336019
Data 4101.0
Data 5501.69897000433602

Example 3:

Consider a database table called Sample with the following records:

Dataxy
Data 122
Data 232
Data 352
Data 41010
Data 55010

To calculate the logarithm of records of column y with base as records of column x, the following query can be used:

SELECT *, LOG(x, y) AS LOG_Value FROM Sample;

This will produce the result as shown below:

DataxyLOG_Value
Data 1221.0
Data 2320.630929753571457
Data 3520.430676558073393
Data 410101.0
Data 550100.588591910067779

❮ SQLite Functions