LOG2 Function in SQL

The LOG2 is a numeric function in SQL which returns the natural logarithm of the number to the base 2.

Syntax of LOG2 Function

In the LOG2 syntax, we have to pass that decimal number whose log base 2 value we want to find.

In the Structured Query Language, we can also use the LOG2 function with the column of the table as shown in the following block:

In this syntax, we have to define the name and column of that table on which we want to perform the LOG2 function.

Examples of LOG2 function

Example 1: This example gets the log 2 of 1:

Output:

Logarithm_of_1
0

Example 2: This example returns the logarithm of 10 by base 2 in the output:

Output:

Logarithm_of_10bybase_2
3.322

Example 3: This example returns the logarithm of 8 by base 2 in the output:

Output:

Logarithm_of_8bybase_2
3

Example 4: This example returns the logarithm of 255 by base 2 in the output:

Output:

Logarithm_of_255bybase_2
7.994

Example 5: This example uses the LOG10 function with the SQL table. In this example, we will create the new table through which we will perform the LOG10 function on the numeric fields of the table:

The following shows the syntax to create the new table in SQL:

The following CREATE statement creates the Product_Details table for storing the price and quantity of products:

The following multiple INSERT queries insert the records of products with their selling and purchasing price into the Product_Details table:

The following SELECT statement displays the inserted records of the above Product_Details table:


Product_IDProduct_NameProduct_QuantityPurchasing_PriceSelling_PriceRelease_DateProduct_Rating
104P110945NULL2022-04-30NULL
202P41545752022-01-285
103P21825NULL2022-02-184
111P7255152021-12-259
210P61550702021-10-15NULL
212P8191102502022-01-284
112P10105508352022-04-11NULL

Query 1: The following SELECT query uses the LOG2 function with the Product_Quantity column of the above Product_Details table:

This query shows the LOG2 value of id of each product.

Output:

Product_IDLOG2_Value_of_Product_ID
1046.7
2027.658
1036.687
1116.794
2107.714
2127.714
1126.807

Query 2: The following SELECT query uses the LOG2 function with the Purchasing_Price column of the above Product_Details table:

This query shows the LOG2 value of purchasing price of each product.

Output:

Purchasing_PriceLOG2_Value_of_PurchasingPrice
9459.884
455.492
254.644
52.322
505.644
1106.781
5509.103

Query 3: The following SELECT query uses the LOG2 function with the Product_Rating column of the above Product_Details table:

This query shows the LOG2 value of rating of each product from the above table.

Output:

Product_RatingLOG2_Value_of_productrating
NULL
52.322
42
93.17
NULL
42
NULL

Query 4: The following SELECT query uses the LOG2 function with the and Purchasing_Price and Selling_Price column of the above Product_Details table:

This query shows the LOG2 value of selling price of each product.

Output:

Selling_PriceLOG2_Value_of_SellingPrice
NULL
756.229
NULL
153.907
706.129
2507.966
8359.706





Latest Courses