Lesson

MySQL LN() Function

10 min reading
Free Course

MySQL LN() Function: Computing Base-e Natural Logarithms

The LN() function computes the natural logarithm of a positive number using Euler's number e as the base. In other words, LN(x) answers what power e must be raised to in order to produce x.

Data engineers use LN() for log-scale normalization of skewed revenue data, continuous growth rate formulas, and statistical regression features. In MySQL, LN(x) works identically to the single-argument LOG(x) function.

Syntax

LN(x)
  • x: Numeric expression greater than 0. Values less than or equal to 0 return NULL.

Practical Example

Suppose demo_orders tracks order totals that vary widely from $5 accessories to $12,000 bulk server orders. Analysts often compute LN(total_amount) to compress extreme skew before feeding metrics into forecasting models.

SELECT
    order_id,
    total_amount,
    ROUND(LN(total_amount), 4) AS log_amount
FROM demo_orders
WHERE order_id BETWEEN 901 AND 905;
order_id total_amount log_amount
901 10.00 2.3026
902 100.00 4.6052
903 1000.00 6.9078
904 0.00 NULL
905 -25.00 NULL

Orders 904 and 905 have zero and negative totals (e.g., refunds), so LN() returns NULL instead of failing the whole query.

Common Mistakes & NULL Behavior

  • Zero or negative arguments: In MySQL, LN(0) and LN(-5) return NULL silently rather than raising a math exception. If you need to log-transform columns that can contain 0, shift with LN(val + 1) or filter WHERE val > 0.
  • Expecting base-10 logarithms: LN(100) returns 4.60517, NOT 2. Use LOG10(100) when you want base-10 logarithms.
  • NULL inputs: LN(NULL) returns NULL.

Frequently Asked Questions

Is there any difference between LN(x) and LOG(x) in MySQL?

No. With one argument, LOG(x) and LN(x) are exact synonyms in MySQL and return identical natural logarithms. Use LN() when you want to make base-e intent unmistakable.

How do I multiply values across rows using LN()?

SQL has no built-in PRODUCT() aggregate, so engineers compute EXP(SUM(LN(col))) on strictly positive rows to multiply values across a group.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum