LN()

SQL · Math Functions

Theory

LN returns the natural logarithm, the logarithm with base e (about 2.71828). LN(1) is 0, LN(2.71828) is about 1, and LN(10) is about 2.3026. Its inverse is EXP: EXP(LN(x)) gives x back, which is a handy way to check your understanding.

The natural log is the standard choice for growth and statistics. If revenue grows from ₹1,000 to ₹2,000, LN(2000 / 1000) = 0.6931 is the continuous growth rate, and log growth rates can simply be added across periods. A classic SQL trick uses it to multiply values within a group, which SQL has no PRODUCT aggregate for: EXP(SUM(LN(x))) multiplies all x values (only for positive numbers). LN is available in PostgreSQL, MySQL, Oracle, Snowflake and SQLite builds with math functions. SQL Server has no LN; there, LOG(x) with one argument already means the natural log. Like any logarithm, LN is undefined for zero and negative inputs.

Why we use it

Many natural and financial processes grow continuously, and the natural log turns that multiplicative growth into simple addition. It also lets SQL compute products of values with SUM.

When to use it

Use LN for growth rates, log-transforms in statistical work and the EXP(SUM(LN(x))) product trick. Use LOG10 when people need to read 'orders of magnitude'. Filter out zero and negative values first.

How it works

  1. Input number, e.g. 2000 / 1000 = 2
  2. Is the input zero or negative? Yes: Return NULL or an error. No: Continue.
  3. Find the power of e that gives the input
  4. Return 0.6931
  5. Use EXP() to convert back if needed

Syntax

LN(number)

Example

SELECT LN(2.718281828);
-- ~1

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic