LOG()

SQL · Math Functions

Theory

A logarithm answers the question 'what power must I raise the base to, to get this number?'. With base 10, LOG(1000) is 3 because 10^3 = 1000, and LOG(100) is 2. Logarithms turn multiplication into addition, which is why they compress huge ranges: values from 10 to 10,00,000 become 1 to 6.

Be careful, because the meaning of LOG differs by database. In PostgreSQL and SQLite, LOG(x) is base 10. In SQL Server and MySQL, LOG(x) is the natural logarithm (base e), and you use LOG10(x) for base 10. Oracle and Snowflake require the base: LOG(10, x). Several databases accept a two-argument form, though the argument order varies; in PostgreSQL, MySQL and SQLite it is LOG(base, x), while SQL Server writes LOG(x, base). LOG is undefined for zero and negative numbers. In analytics, logs are used for order-of-magnitude buckets (FLOOR(LOG10(revenue))), for charts of skewed data and for growth calculations.

Why we use it

Business data is often highly skewed: a few customers spend crores while most spend hundreds. Logs shrink that range so values can be compared, bucketed by order of magnitude, or used in models.

When to use it

Use LOG10 when you want 'number of digits' style buckets or a log scale. Use LN for growth rates and statistical formulas. Always state the base explicitly (LOG10, or LOG(10, x)) so the query means the same on every database.

How it works

  1. Input number, e.g. 1000
  2. Is the input zero or negative? Yes: Return NULL or an error. No: Continue.
  3. Is LOG(x) base 10 here? Yes: Yes: PostgreSQL, SQLite. No: No: SQL Server, MySQL — use LOG10.
  4. Find the power of the base that gives the input
  5. Return 3

Syntax

LOG(number)

Example

SELECT LOG(1000);
-- 3

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