// math reference

SQL ACOS function

Returns the arc cosine in radians for input in [-1, 1]: ACOS(1) is 0 and ACOS(-1) is pi.

Kategorie
Math
Returns
float / double (radians)
In this category
35 of 82

Syntax

ACOS(number)

Engine-Unterstützung

EngineSupportNotizen
SQL Server Ja Supported under this name.
PostgreSQL Ja Supported under this name.
MySQL Ja Supported under this name.
SQLite Ja Requires SQLite 3.35+ with the math functions built in
Oracle Ja Supported under this name.

Beispiele

Any engine

SELECT ACOS(1) AS zero, ACOS(-1) AS pi_value;  -- 0, 3.14159...

Oracle

SELECT ACOS(-1) AS pi_value FROM dual;  -- 3.14159265...

PostgreSQL

SELECT DEGREES(ACOS(0.5)) AS sixty;  -- 60.00000000000001

Gut zu wissen

  • Domain [-1, 1] like ASIN; outside it MySQL and SQLite return NULL, while PostgreSQL, Oracle and SQL Server raise an error.
  • ACOS(-1) is the standard way to obtain pi where PI() is missing, which means every Oracle release.
  • Distance formulas feed ACOS values like 1.0000000000000002 from float error. Clamp before calling: LEAST(1, GREATEST(-1, x)), or MAX(-1, MIN(1, x)) on SQLite.

Questions about ACOS

Why do people write ACOS(-1) for pi?

ACOS(-1) returns pi to the precision of the type, and the expression works on every engine. On Oracle it is the only option, because Oracle has no PI() function.

What does ACOS(2) return?

Nothing valid: NULL on MySQL and SQLite, an error on PostgreSQL, Oracle and SQL Server. The arc cosine only exists for inputs between -1 and 1.

Is the ACOS result in radians?

Yes, always. ACOS(0.5) is about 1.047 radians, which is 60 degrees; use DEGREES() for degree output.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.