SQL ABS
Return the absolute value (remove the minus sign).
Syntax
ABS(number)
Useful for differences where only the size matters.
Example
Query (on the shop database used in the SQL course)
SELECT name, price, ABS(price - 100) AS distance_from_100 FROM products ORDER BY distance_from_100 LIMIT 3;
Result
| name | price | distance_from_100 |
|---|---|---|
| Deep Learning (book) | 89.0 | 11.0 |
| Mechanical keyboard | 119.0 | 19.0 |
| Desk lamp | 59.0 | 41.0 |
3 rows
Related
Learn it step by step in the free lesson Functions: Text, Numbers, Dates & CASE, with exercises that check your queries.