×

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 Advance function

The advance function operates numerical values, string values, and data types.  The advance function converts, displays, and compares given values as per requirement. Here, you find out database, table, and...

4 minutes read.

MySQL PERIOD_DIFF() function

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

2 minutes read.

MySQL RAND() function

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

3 minutes read.

MySQL Queries

MySQL Queries MySQL supports SQL queries in the MySQL interface. These queries help to interact data with the application. MySQL uses create database, user database, create a table, truncate table, and...

13 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: Entity-Relationship Model

Entity-Relationship Model Entity-Relationship model or E R model is used to create a relationship between different attributes or entities. It describes the structure of the database with the help of the...

5 minutes read.

MySQL CROSS JOIN

MySQL CROSS JOIN combines all possibilities of the two or more tables and returns the result that contains every row from all contributing tables. It links several columns from the...

4 minutes read.

MySQL Union

MySQL contains the same categories of data in a different table. Sometimes, you require multiple tables to collect data together. MySQL union is a function to combine two table's data...

5 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 Invisible Index

Invisible Index MySQL index decides an option to the availability of the index for the query optimizer. This index shows the visible and invisible options to displays the index column in...

3 minutes read.

MySQL Descending Index

Descending Index It is a type of index that stores key values in descending order. This query improves the performance of an index query. MySQL system displays the index as per...

3 minutes read.

MySQL function

MySQL function is a piece of program to perform some specific task. Here we pass a parameter and then return a single value. MySQL function mainly supports string, numeric, conditional,...

9 minutes read.

MySQL ELT() function

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

2 minutes read.

MySQL Composite Index

Composite Index A composite index is an index that is used on multiple columns. MySQL management system manages multiple columns simultaneously. Therefore, the single index contains multiple columns in one query...

3 minutes read.

MySQL Full text index

Full text index The full-text index in the table isused to search the full text of the data. This index assigns to the table using a "FULLTEXT" keyword. First, the table...

3 minutes read.

MySQL Operators

MySQL Operators MySQL operator needs advanced operation on the table and its data. This operator works with the "WHERE" clause. MySQL operators are a statement to modify information. It helps to...

13 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 View

Introduction MySQL View is a virtual table to create a clone of the base table. The View does not contain its values or data. MySQL View creates to connect more than...

12 minutes read.

MySQL Math Function

The math function operates numerical values and displays required number data. The math function finds out sin, cos, tan values. Here, you find out the absolute value, reminder, and logarithmic...

6 minutes read.

MySQL Features

Features of MySQL MySQL is a relational database management system (RDBMS): It is a collection of many programs and makes relations with many other programs. Easy to use MySQL database: MySQL...

2 minutes read.