The ABS() function in SQL is a mathematical function used to return the absolute value of a number. It converts negative values into positive values while keeping positive values and zero unchanged.
- Useful for calculating differences and analyzing numerical data.
- Used with both positive and negative numbers.
Syntax
ABS(number)- number: The numeric value for which you want to find the absolute value.
Note: The ABS() function is supported by MySQL, SQL Server, SQLite, PostgreSQL and Oracle.
Working
Below are some examples of the ABS() function to understand how it is used with numeric values.
Example 1: Find the Absolute Value of a Positive Number
In this example, we will find the absolute value of a positive number.
Query:
SELECT ABS(50) AS absolute_value;Output:

Example 2: Find the Absolute Value of a Negative Number
In this example, we will convert a negative value into its corresponding positive value.
Query:
SELECT ABS(-75) AS absolute_value;Output:

Example 3: Use ABS() on a Table Column
Let us create a transactions table containing transaction amounts.

Now, we can use the ABS() function to return the absolute value of each transaction amount.
Query:
SELECT transaction_id,amount,ABS(amount) AS absolute_amountFROM transactions;
Output:

- Returns the absolute value of each amount.
- Negative amounts are converted into positive values.
- The result is displayed as absolute_amount.