LOG() function in MySQL is used to calculate the natural logarithm of a specific number. The number must be >0 Otherwise it will return NULL.
Syntax :
LOG(X)
Parameter : This method accepts one parameter as mentioned above and described below :
- X : A number whose logarithm value we want to calculate. It should be positive number.
Returns : It returns the natural logarithm of given number x.
Example-1 : Logarithm of given number using LOG() function.
SELECT LOG(5) AS Log_Val ;
Output :
Log_Val |
1.6094379124341003 |
Example-2 : Logarithm of 0 using LOG() function.
SELECT LOG(0) AS Log_Val ;
Output :
Example-3 : The LOG function can also be used to find the logarithmic value of a column data columns. To demonstrate create a table named.
Product :
CREATE TABLE Product(
Product_id INT AUTO_INCREMENT,
Product_name VARCHAR(100) NOT NULL,
Buying_price DECIMAL(13, 2) NOT NULL,
Selling_price DECIMAL(13, 2) NOT NULL,
Service_grade Decimal(6, 2) NOT NULL,
PRIMARY KEY(Product_id)
);
Now inserting some data to the Product table :
INSERT INTO
Product(Product_name, Buying_price, Selling_price, Service_grade)
VALUES
('Touring Bike', 2019.00, 3009.6, 0.89 ),
('Mountain Bike', 3019.50, 4000.56, 1.00 ),
('Road Bike', 1019.20, 2000.56, -0.89 ),
('Road Bicycle', 1019.50, 1500.56, -1.50 ),
('Racing Bicycle', 3019.50, 4000.56, 2.00) ;
So, Our table looks like :
mysql> Select * from Product;
+------------+----------------+--------------+---------------+---------------+
| Product_id | Product_name | Buying_price | Selling_price | Service_grade |
+------------+----------------+--------------+---------------+---------------+
| 1 | Touring Bike | 2019.00 | 3009.60 | 0.89 |
| 2 | Mountain Bike | 3019.50 | 4000.56 | 1.00 |
| 3 | Road Bike | 1019.20 | 2000.56 | -0.89 |
| 4 | Road Bicycle | 1019.50 | 1500.56 | -1.50 |
| 5 | Racing Bicycle | 3019.50 | 4000.56 | 2.00 |
+------------+----------------+--------------+---------------+---------------+
5 rows in set (0.00 sec)
Now, we are going to find the logarithmic values for all the records present in the Service_grade column.
Select Product_id,
Product_name,
Buying_price,
Selling_price,
Service_grade,
LOG(Service_grade) AS GRADELOG
FROM Product;
Output :
+------------+----------------+--------------+---------------+---------------+----------------------+
| Product_id | Product_name | Buying_price | Selling_price | Service_grade | GRADELOG |
+------------+----------------+--------------+---------------+---------------+----------------------+
| 1 | Touring Bike | 2019.00 | 3009.60 | 0.89 | -0.11653381625595151 |
| 2 | Mountain Bike | 3019.50 | 4000.56 | 1.00 | 0 |
| 3 | Road Bike | 1019.20 | 2000.56 | -0.89 | NULL |
| 4 | Road Bicycle | 1019.50 | 1500.56 | -1.50 | NULL |
| 5 | Racing Bicycle | 3019.50 | 4000.56 | 2.00 | 0.6931471805599453 |
+------------+----------------+--------------+---------------+---------------+----------------------+
Similar Reads
LOG2() Function in MySQL In this article, we are going to cover the LOG2() function that means it will calculate the logarithm of a specific number with base 2. Pre-requisite :LOG function LOG2() function in MySQL is used to calculate the natural logarithm of a specific number with base 2. The number must be >0 Otherwise
2 min read
LOG10() Function in MySQL LOG10() function in MySQL is used to calculate the natural logarithm of a specific number with base 10. The number must be greater than 0, otherwise it will return NULL. Syntax : LOG10(X) Parameter : This method accepts one parameter as mentioned above in the syntax and described below : X - A numbe
2 min read
LN() Function in MySQL LN() function : It is the function in MySQL is used to calculate the natural logarithm of a specific number with base e . The number must be greater than 0, otherwise it will return NULL. Syntax : LN(X) Parameter : LN() function accepts one parameter as mentioned above in the syntax and described be
2 min read
MOD() Function in MySQL The MOD() function in MySQL is used to find the remainder of one number divided by another. The MOD() function returns the remainder of dividend divided by divisor. if the divisor is zero, it returns NULL. Syntax: MOD(N, M) or N % M or N MOD M Parameter : MOD() function accepts two parameter as ment
4 min read
LEFT() Function in MySQL The LEFT() function in MySQL is used to extract a specified number of characters from the left side of a given string. It uses its second argument to decide, how many characters it should return. Syntax: LEFT (str, len)Parameter: This function accepts two parameters as mentioned above and described
1 min read
LEAST() Function in MySQL The LEAST() function in MySQL is a versatile tool that returns the smallest (minimum) value from a list of expressions. It can be used with numbers, strings, or even columns of data to find the minimum value according to their data type.In this article, We will learn about LEAST() Function in MySQL
4 min read