ABS() in MySQL — How It Works
What the MySQL ABS() function does, with an example.
Published October 21, 2026
ABS(x) returns the absolute (non-negative) value of a number.
How it works
Useful for differences and distances regardless of sign.
Try it in MySQL
SELECT ABS(-15)
15
SELECT ABS(a - b) FROM nums LIMIT 1
distance
SELECT ABS(NULL)
NULL
Practical examples
Example 1
SELECT ABS(-15);
-- 15Example 2
SELECT ABS(a - b) FROM nums LIMIT 1;
-- distanceExample 3
SELECT ABS(NULL);
-- NULLUse it in a real query
SELECT id, ABS(-15) AS result
FROM your_table
LIMIT 10;Common mistakes
- Using ABS to hide data problems.
- Overflow on the minimum integer value.
- Applying to strings.
Key takeaways
- ABS() in one line: ABS(x) returns the absolute (non-negative) value of a number.
- Try queries in a test database first.
- Check NULL handling and data types.
FAQ
Absolute difference?
ABS(col1 - col2).
Does ABS() work in other databases?
Most have an equivalent, but names and syntax vary between MySQL, PostgreSQL, SQL Server and SQLite.