PostgreSQL 18 reference · Math Functions

PostgreSQL ABS() Function

Return the absolute value of a number, so abs(-7) is 7. The result has the same type as the input.

PostgreSQL 18

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

abs(x)

Parameters

ParameterDescription
x A number (integer, numeric or double precision).

Return value

A number of the input type.

Examples

Absolute values
SELECT abs(-7) AS a, abs(-3.50) AS b;

Output

 a |  b
---+------
 7 | 3.50
(1 row)

Run this on your computer — see “Get Started” for setup.

Closest to a target
SELECT name, salary, abs(salary - 120000) AS distance
FROM employees
ORDER BY distance
LIMIT 3;

Output

     name     | salary | distance
--------------+--------+----------
 Priya Nair   | 115000 |     5000
 Lucas Martin | 112000 |     8000
 Jonas Berg   | 140000 |    20000
(3 rows)

Run this on your computer — see “Get Started” for setup.

In other languages

Was this page helpful?