Javatpoint Logo
Javatpoint Logo

LOG Function in SQL

The LOG is a string function in SQL which returns the logarithm of the given number. Or, we can say that, it shows the logarithms of the number to the given base.

Syntax of LOG Function

In the LOG syntax, Number1 must be greater than 0 and it is the number whose log we want to find. Number2 is the base.

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

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

Examples of LOG function

Example 1: This example gets the logarithm of 2:

Output:

Logarithm_of_2
0.69314718055994529

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

Output:

Logarithm_of_2bybase_2
1

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

Output:

Logarithm_of_8bybase_6
1.1606

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

Output:

Logarithm_of_255bybase_4
3.997

Example 5: This example uses the LOG function with the SQL table. In this example, we will create the new table through which we will perform the LOG function on the columns 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_ID Product_Name Product_Quantity Purchasing_Price Selling_Price Release_Date Product_Rating
104 P1 10 945 NULL 2022-04-30 NULL
202 P4 15 45 75 2022-01-28 5
103 P2 18 25 NULL 2022-02-18 4
111 P7 25 5 15 2021-12-25 9
210 P6 15 50 70 2021-10-15 NULL
212 P8 19 110 250 2022-01-28 4
112 P10 10 550 835 2022-04-11 NULL

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

This query gives the logarithm of each product_id by base 4.

Output:

Product_ID Logarithm_of_ProductID_bybase_4
104 3.35
202 3.829
103 3.343
111 3.397
210 3.857
212 3.864
112 3.404

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

This query gives the logarithm of each Purchasing_Price by base 10.

Output:

Purchasing_Price Logarithm_of_Purchasing_Price_bybase_10
945 2.9754
45 1.6532
25 1.398
5 0.699
50 1.699
110 2.0414
550 2.7404

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

This query gives the logarithm of each rating of products by base 6.

Output:

Product_Rating Logarithm_ofratingbybase_6
NULL -
5 0.8982
4 0.7737
9 1.2263
NULL -
4 0.7737
NULL -

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

This query gives the logarithm of each Selling_Price by base 4.

Output:

Selling_Price Logarithm_of_ProductID_bybase_4
NULL -
75 3.1144
NULL -
15 1.9534
70 3.0646
250 3.983
835 4.853






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA