On this page
Syntax
ABS(number)ABS(column)ABS(expression) | Parameter | Type | Required | Description |
|---|---|---|---|
number |
numeric | yes | A numeric literal whose magnitude you want, such as ABS(-7). |
column |
numeric column | yes | A column of integer, decimal or floating point values. |
expression |
numeric expression | yes | Any expression that evaluates to a number, such as ABS(a - b) for a difference. |
How it works
ABS() returns the absolute value of a number, meaning its distance from zero regardless of sign. A negative input becomes positive, a positive input is returned unchanged, and zero stays zero. So ABS(-42) and ABS(42) both return 42.
It is part of the SQL standard and behaves the same across MySQL, PostgreSQL, SQL Server and SQLite, so you can rely on it in portable queries. The result keeps the data type of the input: ABS() of an integer is an integer, and ABS() of a decimal keeps its precision and scale.
The most common uses are measuring differences and distances, where the direction does not matter. ABS(a - b) gives the gap between two values as a single positive number, which is handy for tolerance checks such as ABS(a - b) < 0.01. It pairs naturally with ROUND() for display and with MOD() when you need the magnitude of a remainder.
Examples
Absolute difference between two columns
-- How far apart are the forecast and the actual value,
-- regardless of which one is larger
SELECT product,
ABS(forecast - actual) AS gap
FROM sales
ORDER BY gap DESC;
product | gap --------+----- Widget | 35 Gadget | 12 Bolt | 4
Magnitude of a delta
-- Show the size of each stock change, ignoring
-- whether it was an increase or a decrease
SELECT item,
change_qty,
ABS(change_qty) AS magnitude
FROM inventory_moves;
item | change_qty | magnitude ------+------------+---------- Nail | -18 | 18 Screw| 25 | 25 Nut | -6 | 6
Filter rows within a tolerance
-- Keep only measurements that are within 0.01
-- of the target, in either direction
SELECT sample_id, reading
FROM measurements
WHERE ABS(reading - target) < 0.01;
sample_id | reading
----------+--------
3 | 5.004
7 | 4.998ABS on a negative literal
SELECT ABS(-15) AS a,
ABS(15) AS b,
ABS(0) AS c;
a | b | c ---+----+--- 15 | 15 | 0
ABS with a decimal keeps its scale
SELECT ABS(-3.14159) AS pi_magnitude;
pi_magnitude
------------
3.14159Common mistakes
-- Expecting 0 for a missing value, but ABS of
-- NULL is NULL, so the row is filtered out
SELECT id
FROM readings
WHERE ABS(reading - target) < 0.01; -- NULL reading disappears
Right
-- Handle NULLs explicitly so they are not silently dropped
SELECT id
FROM readings
WHERE reading IS NOT NULL
AND ABS(reading - target) < 0.01;
ABS(NULL) returns NULL, not 0. Because NULL < 0.01 is unknown, those rows never pass the filter. Check for NULL first, or wrap the input in COALESCE() if a default is appropriate.
-- On a signed INT the most negative value has no
-- positive counterpart, so this can overflow
SELECT ABS(-2147483648); -- INT range is -2147483648 to 2147483647
Right
-- Widen the type before taking the absolute value
SELECT ABS(CAST(-2147483648 AS BIGINT));
The smallest value of a signed integer type has a larger magnitude than the largest positive value, so ABS() of it cannot be represented and may overflow or error. Cast to a wider type such as BIGINT or DECIMAL before calling ABS.
Performance
ABS() is a lightweight scalar function: it inspects one value and flips the sign if needed, so its own cost is negligible compared with reading rows from disk.
The catch is that wrapping a column in ABS() inside a WHERE clause makes the predicate non-sargable, so the database usually cannot use a plain index on that column. A condition like ABS(balance) > 1000 forces a scan that evaluates ABS on every row. If you filter this way often, consider a rewrite such as balance > 1000 OR balance < -1000, or a computed/expression index on ABS(balance) where your engine supports one.
For tolerance joins and range comparisons, remember that ABS(a - b) < t is equivalent to b - t < a AND a < b + t, and the second form can sometimes use an index on a.
Interview questions
What does the ABS() function return?
It returns the absolute value of a number, meaning its distance from zero without a sign. Negative inputs become positive, positive inputs are unchanged and zero stays zero, so ABS(-5) and ABS(5) both return 5.
What does ABS() return when the input is NULL?
It returns NULL. ABS() propagates NULL like most scalar functions, so ABS(NULL) is NULL, not 0. Wrap the argument in COALESCE() if you need a default.
How do you find rows where two values differ by less than a tolerance?
Use ABS on the difference so direction does not matter: WHERE ABS(a - b) < 0.01. This keeps rows where a and b are close in either direction.
SELECT * FROM t
WHERE ABS(a - b) < 0.01;
Is ABS() the same across MySQL, PostgreSQL, SQL Server and SQLite?
Yes. ABS() is part of the SQL standard and behaves identically across all four engines, returning a non-negative value of the same type as its input.
Why might ABS() of an integer overflow?
A signed integer type is asymmetric: its most negative value has a magnitude one larger than its largest positive value. So ABS(-2147483648) on a 32 bit INT cannot be represented and may overflow. Cast to BIGINT or DECIMAL first.