Log Function In Sql in SQL
LOG Function in SQL
The LOG function in SQL is used to return the logarithm of a number. The base of the logarithm can vary depending on how the function is used and the database system.
? Syntax
In MySQL:
LOG(number) -- Natural log (base e)LOG(base, number) -- Logarithm to the specified baseIn SQL Server:
LOG(number) -- Natural log (base e)LOG10(number) -- Base-10 logIn Oracle:
LOG(number) -- Natural log (base e)LOG(base, number) -- Log to specific base (Oracle 12c+)? Examples
1. Natural Log (base e):
SELECT LOG(10) AS natural_log;Result:
| natural_log |
|---|
| 2.302585 |
2. Log with Custom Base:
SELECT LOG(10, 1000) AS log_result;Result:
| log_result |
|---|
| 3 |
Because 10³ = 1000
?? Notes:
The
numbermust be greater than 0.Returns
NULLor error if input is 0 or negative.LOG10()is specifically for base-10 in many databases (e.g., SQL Server, MySQL).
Need help with a specific SQL dialect or more math functions? Let me know!