×

MySQL LOG() function

In this context, we will learn how we can use the MySQL LOG () function with proper syntax and good examples.

Introduction of MySQL LOG() function

The 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 of the MySQL LOG() function

The syntax of the MySQL LOG() function is given as follows:

LOG(Y) 

Parameters or arguments used in MySQL LOG() function:

Only one parameter is accepted by the LOG() function in MySQL, which is given as follows:

Y: It is the number that we want to calculate the logarithm of the number. It should be a positive number.

Returns: It will return the natural logarithm of the given number Y.

Application used for LOG() function:

The LOG() function can be used in the given below MySQL versions:

  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1
  • MySQL 4.0
  • MySQL 3.23

Examples of MySQL LOG() function:

Now, we will look into some examples of MySQL LOG functions.

Example-1 :

Here, we will find the logarithm of the given number using the LOG() function in MySQL.

SELECT LOG( 5 ) AS LOG_Val ;

Output :

LOG_VAL
1.6094379124341003
1 row in set (0.00 sec)

Example-2 :

Here, we will find the logarithm of 0 using the LOG() function in MySQL.

SELECT LOG(0) AS LOG_Val ;

Output :

LOG_VAL
NULL
1 row in set (0.00 sec)

Example-3 :

When we require to find the logarithmic value of column data, then we can take the help of the LOG function. For demonstration, we have created a table named commodity.

CREATE TABLE Commodity(
    Commodity_id INT AUTO_INCREMENT,
    Commodity_name VARCHAR(100) NOT NULL,
    Purchasing_price DECIMAL(13, 2) NOT NULL,
    Selling_price DECIMAL(13, 2) NOT NULL,
    Service_grade Decimal(6, 2) NOT NULL,
    PRIMARY KEY(Commodity_id)
);

After that, we will insert some data into the Commodity table:

INSERT INTO
    Commodity(Commodity_name, Purchasing_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) ;

Now, we can view the table using the following query:

mysql> Select * from Commodity;
Commodity_idCommodity_namePurchasing_priceSelling_priceService_grade
          1Touring Bike2019.003009.600.89
          2Mountain Bike3019.504000.561.00
          3Road Bike1019.202000.56-0.89
          4Road Bicycle1019.501500.56-1.50
          5Racing Bicycle3019.504000.562.00

Now, we are going to find the logarithmic values for all the records present in the Service_grade column.

Select Commodity_id,  
 Commodity_name,  
 Purchasing_price,  
 Selling_price,  
 Service_grade,
 LOG(Service_grade) AS GRADELOG  
 FROM Commodity;

The Output is:

Commodity_idCommodity_namePurchasing_priceSelling_priceService_gradeGRADELOG
          1Touring Bike2019.003009.600.89-0.11653381625595151
          2Mountain Bike3019.504000.561.000
          3Road Bike1019.202000.56-0.89NULL
          4Road Bicycle1019.501500.56-1.50NULL
          5Racing Bicycle3019.504000.562.000.6931471805599453

Summary:

In this context, we have learned how we can use the MySQL LOG() function to find the LOG values from given arguments respectively.


Related Topics

MySQL Date and Time function

The date function displays day, year, month, time, and current date. It shows the date and time as per the requirement of the applications. The data either store on the...

8 minutes read.

MySQL FROM_BASE64() function

In this context, we will learn how we can use the MySQL FROM_BASE64()() function with proper syntax and good examples. Introduction of MySQL FROM_BASE64()() function Basically, the FROM_BASE64() function In MySQL decodes...

2 minutes read.

MySQL INNER JOIN

MySQL inner join connects two tables by using their common columns. It is the basic join of the MySQL system. It is used to returns only those results from the...

4 minutes read.

MySQL Vs MongoDB

What is MongoDB? MongoDB is a distributed, open-source, cross-platform document-based database which was created to scale and develop applications simply. It was created as a NoSQL database by MongoDB Inc. MongoDB gets...

6 minutes read.

MySQL Index

An index is a widely used method to access table information quickly. MySQL requires an index for operating table data stored in rows and columns. It's an entry point of...

3 minutes read.

MySQL Environmental Setup

The MySQL Environmental Setup MySQL is free, open-source, and cross-platform software, which can be downloaded from its official website. MySQL management system installs on Linux, macOS, and windows. MySQL requires a framework...

6 minutes read.

MySQL DELETE JOIN

Sometimes join becomes complicated and unwanted. In such cases, we can delete the unwanted joins in the tables. You can delete inner join, left join, right join as per requirement....

3 minutes read.

MySQL Command line client Basic Queries

MySQL – command line client Basic Queries There are many queries to work with the MySQL command-line client. Today, we will explore MySQL basic queries and their function. The MySQL query...

8 minutes read.

MySQL ASIN() Function

In this context we will learn how we can use the ASIN() function in MySQL with proper syntax and good example. Introduction of MySQL ASIN() function The arc sine value of a...

2 minutes read.

MySQL Stored Procedure

MySQL creates the "stored procedure" function to operate database information. You can use parameters, blocks, and statements to create a new procedure. The procedure requires a database table to use...

3 minutes read.

MySQL COALESCE function

This statement displays the first non-null value of the table data. The COALESCE function returns a null value when all values of the table are null or do not find...

3 minutes read.

MySQL LENGTH() Function

In this context, we will learn how we can use the MySQL LENGTH() function with proper syntax and good examples. Introduction of MySQL LENGTH() function This function in MySQL is used to...

2 minutes read.

MySQL LOG2() function

In this context, we will learn how we can use the MySQL LOG2() function to calculate the Logarithm of a specific number with base 2 with proper syntax and examples. Introduction...

2 minutes read.

MySQL Data Types

Introduction of the Data types In the data file, we have several information such as names, numbers, marks, and other information. This information specifies categories such as numbers, characters, and decimal...

8 minutes read.

MySQL Control flow function

The control flow function uses values or operands for logical operations. These functions can works on single and multiple conditions. The control flow function is based on the Boolean expression....

3 minutes read.

MySQL RPAD() function

In this context, we will learn how we can use the MySQL RPAD() function with proper syntax and good examples. Introduction of MySQL RPAD() function RPAD() function in MySQL is used to...

2 minutes read.

MySQL INSERT() Function

In this context, we will learn how we can use the MySQL INSERT() function with proper syntax and good examples. Introduction of MySQL INSERT() function Basically, in MySQL, the INSERT function is...

2 minutes read.

MySQL PERIOD_ADD() function

In this context, we will learn how we can use the MySQL PERIOD_ADD() function with proper syntax and good examples. Introduction of MySQL PERIOD_ADD() function In MySQL, PERIOD_ADD() function will help to...

2 minutes read.

MySQL Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are...

5 minutes read.

MySQL FIELD() function

In this context, we will learn how we can use the MySQL FIELD() function with proper syntax and good examples. Introduction of MySQL FIELD() function The index position of a mentioned value...

3 minutes read.