×

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 Database Introduction

MySQL - Database Introduction: The database plays an essential role in MySQL for storing and modifying the data. The database creates and keeps multiple tables. The database organizes and manipulates...

8 minutes read.

find_in_set() function in MySQL

This Function is used for finding the position of a particular string from the list of strings. For suppose if the specified string is repeated multiple times, then this functions...

4 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 RTRIM() function

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

2 minutes read.

MySQL CURRENT_DATE() Function

In this context, we will learn how we can use the MySQL CURRENT_DATE() function to get the current date, and we will see in the two formats, such as strings...

2 minutes read.

MySQL database queries

MySQL database queries Database queries help to create, use, show and, delete the database. A Database is a collection of several tables and data. The database uses SQL language to perform...

3 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 AVG function

This function works on numerical data type values. The average function shows the average value of the data set. If an average function returns a null value, then the row...

5 minutes read.

MySQL SELF JOIN

This join links table with itself. The inner join, a self join, a right join, and a cross join are connected with two or more two tables. But, the "self...

4 minutes read.

MySQL Drop Index

Sometimes we have an index that is not required in data operations. In that case, we can use this statement to remove an existing index from the table. We can...

4 minutes read.

MySQL DATEDIFF() function

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

3 minutes read.

MySQL LEAST function

This statement displays the smallest value of the table. The LEAST function returns a null value when the table contains a null value. If the table contains all numerical values,...

2 minutes read.

MySQL Table Query

MySQL table query A database stored a lot of data and divided them into different relations known as tables. Each database can contain more than one table. These tables are created...

7 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 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 ROUND() function

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

3 minutes read.

MySQL FLOOR() function

In this context, we will learn how we can use the MySQL FLOOR() function with proper syntaxes and examples. Introduction of MySQL Floor() function: FLOOR() function in MySQL is used to return...

3 minutes read.

MySQL Prefix index

Prefix index This index query creates an index column in the string or character column. MySQL system can create multiple indexes in the table. It will create in a table as...

3 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 LN() function

In this context, we will learn how we can use the MySQL LN() function with proper syntax and good examples. Introduction of MySQL LN() function For evaluation of the natural logarithm of...

2 minutes read.