MySQL TAN() – How to find the tangent of an angle in MySQL?

Tan Function

In this tutorial, we will learn about the MySQL TAN() function. While studying high school mathematics, you must have studied how to find the tangent of an angle. Finding the tangent of an angle is an important trigonometric operation that finds use in mathematics, physics, and so on.

Recommended read – MySQL SIN()MySQL ASIN()MySQL COS()

MySQL provides us with the TAN() function to find the tangent of a value where the value is given in radians. It is important to note that we can also get the tangent of an angle by dividing the sine of an angle with the cosine of an angle. 


Syntax of MySQL TAN()

TAN(number);Code language: SQL (Structured Query Language) (sql)

Where ‘number’ is the measure of an angle in radians whose tangent is to be found.


Examples of MySQL TAN()

Let us start with some basic examples. How about we find the tangent of the following – 2.2, 45, and 1. We use the below queries for the same.

SELECT TAN(2.2);
SELECT TAN(45);
SELECT TAN(1);Code language: SQL (Structured Query Language) (sql)

And we get the output as,

MySQL TAN Basic Example

MySQL TAN() With Negative Values

We can also pass negative values to the MySQL TAN() function. Let us see this using the below examples.

SELECT TAN(-0.2); 
SELECT TAN(-0.75); 
SELECT TAN(-50);Code language: SQL (Structured Query Language) (sql)

And we get the output as follows.

Tan Negative Values

MySQL TAN() With PI()

We can also pass mathematical functions like PI() as arguments to the TAN() function. Most of the times, radian values are expressed in terms of 𝜋. Let us see an example of the TAN() function when PI() is passed to it as an argument.

SELECT TAN(PI());Code language: SQL (Structured Query Language) (sql)

And the output is,

Tan Pi

MySQL TAN() With Expressions

In addition to mathematical functions, we can also pass expressions as arguments to TAN(). We can also include TAN() in expressions. Consider the below expression.

1 + 3 tan 𝜋/4

Let us write a query for the above expression using the TAN() function in MySQL.

SELECT 1+3*TAN(PI()/4);Code language: SQL (Structured Query Language) (sql)

And the output is,

Tan Expression

Using MySQL TAN() With Tables

Consider the below ‘Angles’ table. The Angle column contains the measure of angles in radians.

Tan Angles Table
Angles Table

Simple Example

You must have studied in school that the tangent of an angle is the sine of an angle divided by the cosine of an angle. Mathematically, this is given as –

tan x = sin x / cos x

Let us write a query in which the first part divides the values in the column SineOfAngle with the values in the column CosineOfAngle. The result should have an alias TangentOfAngle. The second part of the same query should find the tangent of the values in the Angle column using the TAN() function. The alias for this result should be TAN_OfAngle. The query is –

SELECT SineOfAngle/CosineOfAngle AS TangentOfAngle, TAN(Angle) AS TAN_OfAngle FROM Angles;Code language: SQL (Structured Query Language) (sql)

And we get the output as follows –

Tan Table Example 1

TAN() With The UPDATE Statement

Let us now create a column called TangentOfAngle which stores the tangent of every angle in the Angles table. We will use the ALTER and UPDATE statements for this task. Let us take a look at the queries.

ALTER TABLE Angles ADD TangentOfAngle float; 
UPDATE Angles SET TangentOfAngle=TAN(Angle); 
SELECT * FROM Angles; Code language: SQL (Structured Query Language) (sql)

We add a column named TangentOfAngle with the data type float using the ALTER statement. Next, using the UPDATE statement, we populate the NULL values in the TangentOfAngle column with the tangent of the angle values from the Angle column. Finally, using the SELECT statement, we display our newly updated table. The output is as follows.

Tan Table Example 2

A Complex Example

If you have studied trigonometric formulas, you must have come across the below formula.

tan 2A = (2 tan A) / (1 – tan2 A)

How about proving if this formula is true using the ‘Angles’ table and the TAN() function. Bear with me because we are gonna be writing a complicated query using all the concepts we saw earlier. We will give ‘tan 2A’ an alias called LHS and ‘ 2 tan A / 1 – tan2 A’ an alias called RHS.

SELECT TAN(2*Angle) AS LHS, (2*TAN(Angle))/(1-POW(TAN(Angle),2)) AS RHS FROM Angles;Code language: SQL (Structured Query Language) (sql)

Read the query once more to understand it better by breaking it one term at a time. We get the output as,

Tan Table Example 3

Conclusion

Finding the tangent of an angle is an important trigonometric operation. You will find yourself using the TAN() function every time you deal with data with trigonometric operations.


References