Open In App

LN() Function in MySQL

Improve
Improve
Like Article
Like
Save
Share
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_ID PRODUCT_NAME BUYING_PRICE SELLING_PRICE SERVICE_GRADE
1 Touring Bike 20000.00 30050.00 4.17 
2 Mountain Bike 30005.50 40000.56 10.00 
3 Road Bike 10000.20 21000.56 -3.59 
4 Road Bicycle 15200.50 18000.00 -0.50
5 Racing Bicycle 30500.50 45000.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_ID PRODUCT_NAME BUYING_PRICE SELLING_PRICE SERVICE_GRADE GRADELOGN
1 Touring Bike 20000.00 30050.00 4.17  1.4279160358107101
2 Mountain Bike 30005.50 40000.56 10.00  2.302585092994046
3 Road Bike 10000.20 21000.56 -3.59  NULL
4 Road Bicycle 15200.50 18000.00 -0.50 NULL
5 Racing Bicycle 30500.50 45000.00  3.00 1.0986122886681098

Last Updated : 27 Oct, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads