Open In App

LN() Function in MySQL

Last Updated : 27 Oct, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
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 below as follows. X – A number whose logarithm value with base e we want to calculate. It should be a positive number. Returns : It returns the natural logarithm of given number x with base e. Example-1 : The logarithm of the given number with base e using the LN() function.
SELECT LN(1000) AS Ln_Val ;
Output :
LN_VAL
6.907755278982137
Example-2 : The logarithm of 0 using LN() function.
SELECT LN(0) AS Ln_Val ;
Output :
LN_VAL
NULL
Example-3 : The LN function can also be used to find the logarithmic value with base e of a column data. 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)
);
Inserting into the Product table : Now inserting some data to the Product table –
INSERT INTO  
Product(Product_name, Buying_price, Selling_price, Service_grade)
VALUES
('Touring Bike', 20000.00, 30050.00, 4.17 ),
('Mountain Bike', 30005.50, 40000.56, 10.00 ),
('Road Bike', 10000.20, 21000.56, -3.59 ),
('Road Bicycle', 15200.50, 18000.00, -0.50 ),
('Racing Bicycle', 30500.50, 45000.00, 3.00) ;
Reading data from table : Showing all data in Product Table –
Select * from Product;
Output :
PRODUCT_IDPRODUCT_NAMEBUYING_PRICESELLING_PRICESERVICE_GRADE
1Touring Bike20000.0030050.004.17 
2Mountain Bike30005.5040000.5610.00 
3Road Bike10000.2021000.56-3.59 
4Road Bicycle15200.5018000.00-0.50
5Racing Bicycle30500.5045000.00 3.00
Now, we are going to find the logarithmic values with base e for all the records present in the Service_grade column.
Select Product_id, Product_name, Buying_price,  
Selling_price, Service_grade,
LN(Service_grade) AS GRADELOGN  
FROM Product;
Output :
PRODUCT_IDPRODUCT_NAMEBUYING_PRICESELLING_PRICESERVICE_GRADEGRADELOGN
1Touring Bike20000.0030050.004.17 1.4279160358107101
2Mountain Bike30005.5040000.5610.00 2.302585092994046
3Road Bike10000.2021000.56-3.59 NULL
4Road Bicycle15200.5018000.00-0.50NULL
5Racing Bicycle30500.5045000.00 3.001.0986122886681098

Next Article
Article Tags :

Similar Reads