Home » COS Function in SQL

COS Function in SQL

by

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:

21222324252627282930
0.8366-8.8513-0.846-0.9055-0.13230.76250.95630.27090.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_IDProduct_NameProduct_QuantityPurchasing_PriceSelling_PriceRelease_DateProduct_Rating
104P110.250945NULL2022-04-30NULL
202P415.50045752022-01-285
103P218.25025NULL2022-02-184
111P725.2505152021-12-259
210P615.50050702021-10-15NULL
212P819.7501102502022-01-284
112P1010.2505508352022-04-11NULL

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_IDCos_Value_of_Product_ID
104-0.3216
2020.80641
1030.622
111-0.8654
2100.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_PriceCos_Value_of_PurchasingPriceSelling_PriceCos_Value_of_SellingPrice
9450.58053NULL
450.8509075-0.387781
25-0.132351NULL
5-0.958915-0.650287
50-0.26237470-0.773890
110-4.42426250-0.97052
550-0.21948835-0.61599

You may also like