In this tutorial, we will learn about the MySQL COT()
function. Cotangent or the ‘cot’ function is a trigonometric function that is used to find the cotangent of an angle. The cotangent of an angle is also the reciprocal of the tangent of an angle.
Finding the cotangent of an angle is an important trigonometric operation which finds use in mathematics, physics and so on.
MySQL provides us with the COT()
function to find the cotangent of a value where the value is given in radians. Since cotangent is the reciprocal of the tangent of an angle, we can also find the cotangent of an angle by dividing the cosine of an angle with the sine of an angle.
Recommended read – MySQL TAN() function
Syntax of MySQL COT()
COT(number);
Code language: SQL (Structured Query Language) (sql)
Where ‘number’ is the measure of an angle in radians whose cotangent is to be found.
Examples of MySQL COT()
Let us kick things off with some basic examples. Let us find the cotangent of the following – 33, 0.5 and 1. We use the below queries for the same.
SELECT COT(33);
SELECT COT(0.5);
SELECT COT(1);
Code language: SQL (Structured Query Language) (sql)
And we get the output as,
MySQL COT() With Negative Values
We can also pass negative values to the COT()
function. Let us see this using the below examples.
SELECT COT(-0.5);
SELECT COT(-1);
Code language: SQL (Structured Query Language) (sql)
And we get the output as follows.
COT() With PI()
Since most of the times radian values are expressed in terms of 𝜋, Let us see an example of the COT()
function when MySQL PI()
is passed to it as an argument.
SELECT COT(PI());
Code language: SQL (Structured Query Language) (sql)
And the output is,
COT() With Expressions
In addition to mathematical functions such as PI()
, we can also pass expressions as arguments to COT()
. We can also include COT()
as a part of expressions. Let us see an example of this.
Consider the following query. 3 + 5 cot 𝜋/2
Let us write a MySQL query for the above expression using the MySQL COT() function.
SELECT 3+5*COT(PI()/2);
Code language: SQL (Structured Query Language) (sql)
And the output is,
Using MySQL COT() With Tables
Consider the below ‘Angles’ table. The Angle column contains the measure of angles in radians.
Simple Example 1
As I mentioned earlier and as you would have studied in school, the cotangent of an angle is actually the reciprocal of the tangent of that angle. Mathematically, this is given as – cot x = 1 / tan x
Let us use the TangentOfAngle
in the Angles table and prove this above equation using the SELECT statement. Consider the below query.
SELECT 1/TangentOfAngle AS CotangentOfAngle,
COT(Angle) AS COT_Of_Angle
FROM Angles;
Code language: SQL (Structured Query Language) (sql)
And we get the output as follows –
As you can see, the values in both the columns are equal.
Simple Example 2
You must have also studied in school that the cotangent of an angle is the cosine of an angle divided by the sine of an angle. Mathematically, this is given as – cot x = cos x / sin x
Let us write a query in which the first part divides the values in the column CosineOfAngle
with the values in the column SineOfAngle
. The result should have an alias CotangentOfAngle
.
The second part of the same query should find the cotangent of the values in the Angle column using the MySQL COT()
function. The alias for this result should be COT_Of_Angle
. The query is –
SELECT CosineOfAngle/SineOfAngle AS CotangentOfAngle,
COT(Angle) AS COT_Of_Angle
FROM Angles;
Code language: SQL (Structured Query Language) (sql)
And we get the output as follows –
A Complex Example
If you have studied trigonometric formulas, you must have come across the below formula.
cot 2A = (cot2 A – 1) / (2 cot A)
How about proving if this formula is true using the ‘Angles’ table and the COT()
function. Bear with me because we are gonna be writing a complicated query using all the concepts we saw earlier. We will give ‘cot 2A’ an alias called LHS and ‘(cot2 A – 1) / (2 cot A)’ an alias called RHS.
SELECT COT(2*Angle) AS LHS,
(POW(COT(Angle),2)-1)/(2*COT(Angle)) 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,
Conclusion
Finding the cotangent of an angle is an important trigonometric operation. You will find yourself using the COT()
function every time you deal with data with trigonometric operations.
References
- MySQL Official Documentation on
COT()
.