Lesson

MySQL COS() Function

10 min reading
Free Course

MySQL COS() Function: Calculating Cosine Values in SQL

The COS() function returns the trigonometric cosine of an angle measured in radians. The output is a DOUBLE value ranging from -1 to 1.

Developers mostly use COS() in spatial queries—such as calculating Earth surface distances from latitude coordinates—or in engineering analytics involving periodic waveforms. Because MySQL expects radians, degree inputs must be converted first.

Syntax

COS(x)
  • x: Numeric expression representing an angle in radians.

Practical Example

Suppose demo_customers stores customer latitude in degrees (latitude_deg). In geographic distance math, one degree of longitude shrinks by COS(RADIANS(latitude_deg)). Let's inspect that scaling factor per customer city.

SELECT
    customer_id,
    city,
    latitude_deg,
    ROUND(RADIANS(latitude_deg), 4) AS lat_radians,
    ROUND(COS(RADIANS(latitude_deg)), 4) AS lon_scale_factor
FROM demo_customers
WHERE customer_id IN (1, 2, 3, 4);
customer_id city latitude_deg lat_radians lon_scale_factor
1 Quito 0.00 0.0000 1.0000
2 Miami 25.76 0.4496 0.9006
3 London 51.51 0.8990 0.6224
4 Reykjavik NULL NULL NULL

At the equator (0.00 degrees), COS(0) equals 1.0000. As latitude increases toward London, the cosine shrinks to 0.6224.

Common Mistakes & NULL Behavior

  • Passing degrees without converting: Writing COS(90) computes the cosine of 90 radians (-0.44807), not 90 degrees (0). Always wrap degree columns in RADIANS(deg).
  • Floating-point near-zero results: Because PI() is a finite approximation, COS(PI() / 2) returns 6.123233995736766e-17 instead of exact 0. Use ROUND(COS(expr), 6) before comparing to 0.
  • NULL inputs: Passing NULL to COS() returns NULL, as shown for row 4 above.

Frequently Asked Questions

What happens if I pass a huge number to COS()?

COS() accepts any finite numeric input without domain errors and returns a value in [-1, 1]. Passing NULL is the only normal way to get NULL.

How do I calculate COS() for 60 degrees in MySQL?

Run SELECT COS(RADIANS(60));, which returns 0.5000000000000001 (or 0.5 when rounded).

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum