SQLite 3 reference · Math Functions

SQLite LOG() Function

Return the base-10 logarithm of a number, or the logarithm in any base with log(B, X). log10() and log2() also exist.

SQLite 3 Since 3.35.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

log(X)  |  log(B, X)  |  log10(X)  |  log2(X)

Parameters

ParameterDescription
B optional The base.
X A number (text that looks like a number is converted).

Return value

A floating-point number, or NULL.

Examples

Different bases
SELECT log(1000) AS base10, log(2, 8) AS base2, log2(1024) AS log2_fn;

Output

base10 | base2 | log2_fn
3 | 3 | 10

Run this on your computer — see “Get Started” for setup.

Order of magnitude
SELECT name, price, CAST(log10(price) AS INTEGER) + 1 AS digits_before_point
FROM products
WHERE product_id IN (6, 8, 11);

Output

name | price | digits_before_point
4K Monitor | 329 | 3
Coffee Mug | 12 | 2
Rubber Duck | 5.5 | 1

Run this on your computer — see “Get Started” for setup.

This is one of SQLite's built-in math functions, available since 3.35.0 when SQLite is compiled with SQLITE_ENABLE_MATH_FUNCTIONS (the default for the sqlite3 command-line tool and most language bindings). The in-browser SQLite on this site is built without them, so the examples show output from standard SQLite instead.

Version history

VersionChange
SQLite 3.35.0Added

In other languages

Was this page helpful?