MySQL LOG2 Function

MySQL LOG2 function is one of the Mathematical functions used to return the logarithmic value of a given number with base 2. Let us see how to find base 2 logarithmic values in MySQL using Command Prompt, and Workbench with examples.

MySQL LOG2 Syntax

The basic syntax of the LOG2 in MySQL is as shown below:

SELECT LOG2(x) FROM Source

MySQL LOG2 Function Example 1

The LOG2 Function returns the logarithmic value with base 2 of any numeric value. The following query shows multiple ways to use the LOG2 function.

SELECT LOG2(10);

SELECT LOG2(623250);

SELECT LOG2(250), LOG2(-150);
MySQL LOG2 Function 1

MySQL LOG2 Example 2

The MySQL LOG2 Mathematical functions also allows you to find the base 2 logarithmic value of a column data. In this MySQL example, we are going to find the base 2 logarithmic values for all the records present in Standard Cost, Sales Amount, and Tax Amount columns.

SELECT EnglishProductName, 
Color,
      StandardCost,
      LOG2( StandardCost) AS LogStdCost,
      SalesAmount,
      LOG2( SalesAmount) AS LogSalesAmount,
      TaxAmt,
     LOG2( TaxAmt) AS TaxAmt
 FROM `MySQL Tutorial`.`mathemetical functions`;
MySQL LOG2 Function 2