Lesson

MySQL LOG10() Function

10 min reading
Free Course

MySQL LOG10() Function: Computing Base-10 Logarithms

The LOG10() function returns the base-10 (common) logarithm of any positive number in MySQL. For powers of ten, it produces exact steps: LOG10(10) is 1, LOG10(100) is 2, and LOG10(1000) is 3.

Developers use LOG10() to classify metrics by order of magnitude—such as comparing $10, $100, and $10,000 customer orders on a clean chart scale—or to count digits in positive integers.

Syntax

LOG10(x)
  • x: Numeric expression strictly greater than 0.

Practical Example

Suppose demo_customers tracks lifetime_spend across small retail buyers and enterprise accounts. Using FLOOR(LOG10(lifetime_spend)) groups customers cleanly by power-of-10 tier (1–9, 10–99, 100–999, and 1000+).

SELECT
    customer_id,
    company_name,
    lifetime_spend,
    ROUND(LOG10(lifetime_spend), 2) AS log10_spend,
    FLOOR(LOG10(lifetime_spend)) + 1 AS integer_digits
FROM demo_customers
WHERE lifetime_spend > 0
ORDER BY lifetime_spend DESC;
customer_id company_name lifetime_spend log10_spend integer_digits
51 Northstar Labs 25000.00 4.40 5
52 Bright Studio 850.00 2.93 3
53 Corner Cafe 45.00 1.65 2

Notice that FLOOR(LOG10(x)) + 1 yields the exact number of whole digits (5 digits for 25000, 3 for 850).

Common Mistakes & NULL Behavior

  • Zero and negative inputs: Calling LOG10(0) or LOG10(-100) returns NULL in MySQL. Always filter WHERE col > 0 before using LOG10(col) inside GROUP BY or arithmetic formulas.
  • Fractional values below 1: LOG10(0.01) is valid and returns -2. Do not assume LOG10() only works on numbers >= 1; only <= 0 returns NULL.
  • NULL propagation: LOG10(NULL) returns NULL.

Frequently Asked Questions

What is the difference between LOG(100) and LOG10(100)?

LOG(100) uses base e and returns 4.60517, while LOG10(100) uses base 10 and returns 2.

How do I reverse a LOG10() calculation in SQL?

Raise 10 to the logged value using POW(10, log_val). For example, POW(10, 3) restores 1000.

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