COS()

SQL · Math Functions

Theory

COS returns the trigonometric cosine of an angle. In a right-angled triangle, cosine is the adjacent side divided by the hypotenuse, so like sine it stays between -1 and 1. COS(0) is 1, the cosine of 60 degrees is 0.5 and the cosine of 90 degrees is 0.

COS also expects radians, so convert degrees with RADIANS() or multiply by PI() / 180. A practical way to picture it: if a force of 100 units pulls at 60 degrees from the ground, the part that pulls horizontally is 100 × COS(60°) = 50. COS is often paired with SIN: in the haversine formula for distance between two latitude/longitude points, and in data science where a cyclical value such as hour-of-day is encoded as SIN and COS so that 23:00 and 00:00 end up close together. Because of floating-point maths, COS(RADIANS(90)) returns a tiny number like 6e-17 instead of exactly 0, so round the result.

Why we use it

Many formulas need to split a length or force into horizontal and vertical parts, or measure distance on a sphere. COS gives the horizontal share of an angle and pairs with SIN for the vertical share.

When to use it

Use COS in geospatial distance formulas, direction or force calculations and cyclical feature encoding. Convert degrees to radians first, and ROUND results so tiny floating-point leftovers do not show as odd values.

How it works

  1. Angle in degrees, e.g. 60
  2. Convert with RADIANS(60) = 1.0472
  3. Compute the cosine
  4. Is the result a tiny leftover like 6e-17? Yes: ROUND it to 0. No: Use the value.
  5. Return a value between -1 and 1 (0.5)

Syntax

COS(radians)

Example

SELECT COS(0);
-- 1

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic