SQL - LOG10() Function
The SQL LOG10() Is a mathematical function that is used in the SQL to retrieve the natural logarithm of a specified number to the base-10.
Syntax
Following is the syntax of the SQL LOG10() function −
LOG10(X);
Parameters
X − is the numeric value required to calculate the natural logarithm of base 10.
Syntax
Following is the syntax to use the LOG10() in the SQL table −
SELECT column_name, LOG10(X) AS alias_name From table_name;
In the above SQL query, we can pass the column name from the table in which we want to perform the LOG10() function.
Example
In the following example, we are calculating of log(10) to the base 10. It returns 1.
SELECT LOG10(10) AS 'log10';
Output
Following is the output of the above −
+-------+ | log10 | +-------+ | 1 | +-------+
Example
In the following example, we are passing 0 and a negative value to the LOG10() function if the value passed is 0 and a negative. Log10 will return a value null. In MySQL, it gives an error in the SQL.
Following is the query −
SELECT lOG10(0) AS 'LOG(0)', LOG10(-2) AS 'LOG(-2)';
Output
On execution of the above SQL query, we get the error because the LOG10() function in SQL returns an error if the argument is 0 or a negative value.
An invalid floating point operation occurred.
Example
In the following example, we are finding the value of log10(1) using the SQL Log10() function.
SELECT LOG10(1) AS log1;
Output
When we execute the above SQL query, we get 0 because log1 always returns 0.
+------+ | log1 | +------+ | 0 | +------+
Example
In the following example, we are passing the floating-point number and also passing the string-type number in the SQL log10() function.
SELECT lOG10('4') AS log4, LOG10(255.255) AS log_Of_Floatvalue;
Output
Following is the output of the above SQL query −
+--------------------+-------------------+ | log4 | log_Of_Floatvalue | +--------------------+-------------------+ | 0.602059991327962 | 2.40697425791327 | +--------------------+-------------------+
Example
In the following example, we are using the emp_tbl to fetch the ID and name and perform the LOG10(). Here, we are calculating the LOG10 value of the ID of the emp_tbl table.
Letâs create a table named emp_tbl −
Create table emp_tbl (ID INT NOT NULL, NAME VARCHAR(20), ACCOUNT_BL NUMERIC);
Letâs insert n data into table &mins;
insert into emp_tbl values(1, 'Raja', 1200); insert into emp_tbl values(2, 'Vivek', 1500); insert into emp_tbl values(3, 'Roja', -1500); insert into emp_tbl values(4, 'Lukha', -1700); insert into emp_tbl values(5, 'Sonal', 1800);
Letâs fetch the table details.
SELECT * FROM emp_tbl;
Following is the emp_tbl table −
+----+-------+------------+ | ID | NAME | ACCOUNT_BL | +----+-------+------------+ | 1 | Raja | 1200 | | 2 | Vivek | 1500 | | 3 | Roja | -1500 | | 4 | Lukha | -1700 | | 5 | Sonal | 1800 | +----+-------+------------+
Following is the query to Fetch the Name, ID and calculate the LOG10() of the ID −
SELECT NAME, ID, LOG10(ID) log_Of_ID from emp_tbl;
Output
Following is the output of the above SQL query −
+-------+----+---------------------+ | NAME | ID | log_Of_ID | +-------+----+---------------------+ | Raja | 1 | 0 | | Vivek | 2 | 0.3010299956639812 | | Roja | 3 | 0.47712125471966244 | | Lukha | 4 | 0.6020599913279624 | | Sonal | 5 | 0.6989700043360189 | +-------+----+---------------------+