Javatpoint Logo
Javatpoint Logo

COS Function in SQL

The COS is a SQL function of mathematics which returns the cosine value of the specified number.

Syntax of COS Function

In the COS syntax, we have to pass that decimal number whose cosine value we want to return.

In the Structured Query Language, we can also use the COS function with the field 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 COS function.

Examples of COS function

Example 1: This example returns the cosine value of the number in the given query:

Output:

Cos_Value_of_101
0.89200

Example 2: This example returns the cosine value of 2 from the following SELECT query:

Output:

Cos_Value_of_2
0.41614683654714241

Example 3: This example returns the cosine value of 8:

Output:

Cos_Value_of_8
0.98935824662338179

Example 4: This example returns the Cos_Value of 255:

Output:

Cos_Value_of_255
-0.50639163492449091

Example 5: This example returns the Cos_Value of 90:

Output:

Cos_Value_of_90
-0.448073616

Example 6: This example returns the Cos_Value of NULL:

Output:

Cos_Value_of_NULL
-

Example 7: This example returns the cosine value of all the numbers between 21 and 30:

Output:

21 22 23 24 25 26 27 28 29 30
0.8366 -8.8513 -0.846 -0.9055 -0.1323 0.7625 0.9563 0.2709 0.6636 -0.988

Example 8: This example uses the COS function with the SQL table.

In this example, we will create the new table through which we will perform the COS function on numerical columns:

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.250 945 NULL 2022-04-30 NULL
202 P4 15.500 45 75 2022-01-28 5
103 P2 18.250 25 NULL 2022-02-18 4
111 P7 25.250 5 15 2021-12-25 9
210 P6 15.500 50 70 2021-10-15 NULL
212 P8 19.750 110 250 2022-01-28 4
112 P10 10.250 550 835 2022-04-11 NULL

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

This query returns the cosine value of each product id from the table.

Output:

Product_ID Cos_Value_of_Product_ID
104 -0.3216
202 0.80641
103 0.622
111 -0.8654
210 0.4677185
212 -0.99834
112 -0.8899956

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

This query shows the cosine value of purchasing and selling price of each product.

Output:

Purchasing_Price Cos_Value_of_PurchasingPrice Selling_Price Cos_Value_of_SellingPrice
945 0.58053 NULL
45 0.85090 75 -0.387781
25 -0.132351 NULL
5 -0.9589 15 -0.650287
50 -0.262374 70 -0.773890
110 -4.42426 250 -0.97052
550 -0.21948 835 -0.61599






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