SQL - SIN() Function



The SQL SIN() function is a mathematical function that retrieves the sine value of a numerical value. It accepts an integer as a parameter as well as angles such as PI()/2, PI()/3, PI()/4, and PI()/6 and returns the float expression which is the the sin value (in radians).

Syntax

Following is the syntax of the SQL SIN() function −

SELECT SIN(X) AS alias_name

Where x is the numeric value which returns by the SIN() in radians.

Following is the syntax to use the SIN function in a SQL table −

SELECT SIN(Integer_column_name) AS Alias_Name FROM table_name;

We can use the above syntax, to find the sine value of the column of a table.

Example

In the following example, we are calculating the sine value of the radian 1.

Following is the query −

SELECT SIN(1) AS sine_value;

Output

On execution of the above SQL query, we get the sine value of the 1.

+--------------------+
| sine_value         |
+--------------------+
| 0.8414709848078965 |
+--------------------+

Example

In the following example, we can also pass the PI() function to the SIN() function to calculate the value of SIN(PI()) in radians.

Following is the query −

SELECT SIN(PI()) AS sine_value;

Output

On execution of the above SQL query, we get the value of the SIN(PI()) as follow:

+------------------------+
| SIN(PI())              |
+------------------------+
| 1.2246467991473532e-16 |
+------------------------+

Example

In the following example, we are calculating the sine value of the null using the SIN() function. Following is the query −

SELECT SIN(NULL) as sine_of_null;

Output

When we run the above SQL query, we get the null value because if we pass a null value to the sin() function, it will only return the null value.

+--------------+
| sine_of_null |
+--------------+
|         NULL |
+--------------+

Example

In the following example, we are calculating the sine value of the 0 using the SIN() function.

Following is the query −

SELECT SIN(0) as sine_of_zero;

Output

When we run the above SQL query, we get the 0 value because if we pass a 0 value to the sin() function, it will only return the 0 value.

+--------------+
| sine_of_zero |
+--------------+
|            0 |
+--------------+

Example

In the following example, we are fetching the ID, NAME and calculating the sine value of the age using the SIN() function from the customers table.

Let’s create a table named customers using the CREATE statement −

CREATE TABLE customers(ID INT NOT NULL PRIMARY KEY(ID), 
NAME VARCHAR(30) NOT NULL, 
AGE INT NOT NULL, ADDRESS CHAR(30), 
SALARY DECIMAL(18, 2));

Let’s insert the data into the CUSTOMERS using the INSERT statement −

insert INTO customers VALUES(1, 'Ramesh', 32, 'Ahmedabad', 2000);
insert INTO customers VALUES(2, 'Aman' 23, 'Ranchi', 40000);
insert INTO customers VALUES(3, 'kaushik', 23, 'Kota', 2000);
insert INTO customers VALUES(4, 'Chaitali', 25, 'Mumbai', 6500);
insert INTO customers VALUES(5, 'Rakesh', 24, 'Kota', 30000);
insert INTO customers VALUES(6, 'Vivek', 22, 'Ranchi', 35000);
insert INTO customers VALUES(7, 'Akash', 22, 'Ranchi', 50000);

Following is the customers table −

+------+----------+------+-----------+--------+
| ID   | NAME     | AGE  | ADDRESS   | SALARY |
+------+----------+------+-----------+--------+
|    1 | Ramesh   |   32 | Ahmedabad |   2000 |
|    2 | Aman     |   23 | Ranchi    |  40000 |
|    3 | kaushik  |   23 | Kota      |   2000 |
|    4 | chsitali |   25 | Mumbai    |   6500 |
|    5 | Rakesh   |   24 | kota      |  30000 |
|    6 | Vivek    |   22 | Ranchi    |  35000 |
|    7 | Akash    |   22 | Ranchi    |  50000 |
+------+----------+------+-----------+--------+

Following is the query to fetch the details −

SELECT ID, NAME, SIN(AGE) as sine_of_AGE FROM customers;

Output

When we run the above SQL query, we get the following information: ID, NAME, and the sine value of the age −

+------+----------+-----------------------+
| ID   | NAME     | sine_of_AGE           |
+------+----------+-----------------------+
|    1 | Ramesh   |    0.5514266812416906 |
|    2 | Aman     |   -0.8462204041751706 |
|    3 | kaushik  |   -0.8462204041751706 |
|    4 | Chaitali |  -0.13235175009777303 |
|    5 | Rakesh   |   -0.9055783620066238 |
|    6 | Vivek    | -0.008851309290403876 |
|    7 | Akash    | -0.008851309290403876 |
+------+----------+-----------------------+
sql-numeric-functions.htm
Advertisements