Knowledge base / SQL

LOG in SQL: logarithm bases and argument order

I check the engine before using LOG. Here are the different default bases, SQL Server's reversed argument order and decisions for zeros and missing values.

Kajo RudzińskiPublished Updated 4 min read

Towers of increasing height pass through a prism into evenly spaced steps in pixel art.

Before using LOG, I check the database engine. With one argument, the same name can mean a different logarithm; with two arguments, SQL Server reverses their order compared with several other engines. Copying a query without that context can produce a valid statement with an unintended result. I therefore separate the dialects rather than present one universal SQL signature.

What a logarithm means

A logarithm asks which power of a base produces a given number. For base 10 and number 100, the answer is 2, because ten squared is 100. For base 2 and number 8, it is 3. I use small synthetic values like these to check the reasoning without relying on long decimal approximations.

The number must be positive. A real logarithm’s base must be positive and different from one. The logarithm of one is zero for every valid base. Values between zero and one are allowed; with a base greater than one, they give negative results. A negative logarithm is therefore not evidence of an invalid input by itself.

Choose the syntax for the engine

Documented environment Natural logarithm Base-10 logarithm Another base
PostgreSQL 18 LN(x) LOG(x) LOG(b, x)
MySQL 8.4 LN(x) or LOG(x) LOG10(x) LOG(b, x)
SQL Server 2012 and later LOG(x) LOG10(x) LOG(x, b)
Oracle Database 19c LN(x) LOG(10, x) LOG(b, x)
SQLite with mathematical functions LN(x) LOG(x) or LOG10(x) LOG(b, x)

See the corresponding documentation for PostgreSQL, MySQL, SQL Server, Oracle and SQLite. SQLite’s mathematical functions require the relevant compilation option. Microsoft’s documentation also lists a separate Azure Synapse SQL signature, so I would not automatically apply SQL Server’s two-argument form there.

Implementation limits can be narrower than the mathematical definition. MySQL 8.4 documents NULL for bases at or below one, and SQL Server takes an integer base. The examples use bases 2 and 10, which satisfy these restrictions.

One calculation with different notation

In PostgreSQL 18 and MySQL 8.4, write the base-10 logarithm of 100 as follows:

SELECT LOG(10, 100) AS result;

SQL Server 2012 and later use the opposite argument order:

SELECT LOG(100, 10) AS result;

For Oracle 19c, include DUAL:

SELECT LOG(10, 100) AS result FROM DUAL;

The mathematical result is 2. In SQL Server, however, LOG(10, 100) uses base 100 and number 10, giving 0.5. You can reason about this directly: the square root of 100 is 10. Reversing the arguments can be harder to notice than a syntax error because the returned number may still look plausible.

Decide how to handle zero and missing values

Do not assume a universal error response. MySQL returns NULL and a warning for a nonpositive LOG input, while SQLite returns NULL for domain errors. Another engine may raise an error. In PostgreSQL, I would avoid taking a logarithm of zero or a negative number. First establish what those records mean for the analysis and how they should be represented afterwards.

For a column of synthetic measurements, this PostgreSQL 18 example explicitly leaves an absent result for invalid inputs:

SELECT
    value,
    CASE WHEN value > 0 THEN LN(value) ELSE NULL END AS ln_value
FROM measurements;

One gives zero. Zero, a negative value and a missing measurement leave NULL. That is the report’s chosen rule and needs an explanation. If you subsequently average only available transformations, the included set of observations changes. I would also show the excluded measurement count so that an incomplete transformed result does not appear to cover the entire source.

Different inputs pass through separate gates representing the choice of logarithm base.

When the transformation is useful

For synthetic values 1, 10, 100 and 1000, base-10 logarithms are 0, 1, 2 and 3. Successive tenfold increases become equal steps. That can help compare values across orders of magnitude. Label the transformation clearly: a one-point difference on this scale does not mean that the original quantity increased by one unit.

I would not describe this as automatic normalisation or a guaranteed repair of a distribution. The transformation changes interpretation and needs a reason. Adding one to every observation just to accommodate zeros also changes the definition; explain that decision if it is appropriate for the task. Use the same base and input rules when comparing groups.

A short exercise

Choose one engine from the table. Write its correct signature for base 2 and values 1, 2 and 8. The expected results are 0, 1 and 3. Then describe what you will do with zero and a missing measurement. The aim is to explain the base, argument order and exclusions before including the calculation in a larger report.

For another transformation, read SQRT in SQL. For summarising selected records, revisit WHERE and GROUP BY. I would keep a short base label and an excluded-value count in the final report. They let a reader interpret the result without guessing what the author intended by LOG alone.

Key takeaways

Does LOG always calculate a natural logarithm?

No. PostgreSQL and SQLite use LOG(x) for base 10. MySQL and SQL Server default to a natural logarithm; Oracle LOG requires an explicit base.

How do I calculate the base-10 logarithm of 100 in SQL Server?

Use LOG(100, 10) in SQL Server 2012 and later. The number comes first and the base second; the mathematical result is 2.

Does taking LOG of zero always raise an error?

The engines differ. MySQL returns NULL and a warning, while SQLite returns NULL for domain errors. Define an input policy and consult your engine's documentation.

AI was used in an automated way while working on this article. If you want to learn how to do it too, check out this project.

Kajo Rudziński

Newsletter KajoData

Let’s keep in touch.

I write about data analysis, learning and changing careers. My newsletter and the sign-up form are in Polish. If that works for you, I’d be glad to see you there.