Skip to main content
Log inGet a demo
Back to SQL Dictionary
Aggregate functions

SQL ABS

What is SQL ABS?

The SQL ABS function is used to calculate the absolute (non-negative) value of a numeric expression or column in a database table. It returns the magnitude or positive value of the number, effectively removing any negative sign.

When you would use it

You would use the SQL ABS function when you need to work with the absolute value of a numeric value. This can be useful in various situations, such as calculating the absolute difference between two values or ensuring that a value is treated as positive, regardless of its sign.

Syntax

The syntax for using the ABS function is as follows:

SELECT ABS(expression) FROM table_name WHERE condition;
  • expression: The numeric expression or column for which you want to calculate the absolute value.
  • table_name: The name of the table containing the data.
  • condition (optional): An optional condition to filter the rows for which you want to calculate the absolute value.

Parameter values

  • expression: A numeric value or column containing numeric values.
  • table_name: The name of the table where the data is stored.
  • condition (optional): A condition that filters the rows if you want to apply additional filtering before calculating the absolute value.

Example query

Suppose we have a table named "temperature_readings" with columns "reading_id" and "temperature." We want to calculate the absolute difference between the recorded temperature and a reference temperature (e.g., 25 degrees):

SELECT ABS(temperature - 25) AS temp_difference
FROM temperature_readings
-- Comment the line below to show it doesn't affect the query.
-- WHERE location = 'Living Room';

In the above query, we use the ABS function to calculate the absolute temperature difference from the reference value.

Example table response

Assuming the "temperature_readings" table contains the following data:

| reading_id | temperature |
|------------|------------ |
| 1         | 22.5       |
| 2         | 26.0       |
| 3         | 25.5       |
| 4         | 24.0       |

The query mentioned earlier would return the following result:

| temp_difference |
|---------------- |
| 2.5            |
| 1.0            |
| 0.5            |
| 1.0            |

This shows the absolute temperature difference between the recorded temperatures and the reference value of 25 degrees.

Use cases

  • Calculating the absolute difference between values, useful in various mathematical and scientific applications.
  • Ensuring that a value is treated as positive, regardless of its original sign.
  • Handling scenarios where negative values need to be considered in a positive context.

SQL languages this is available for

The SQL ABS function is a standard SQL feature and is available in most relational database management systems (RDBMS) that support SQL. This includes popular RDBMS like MySQL, PostgreSQL, Oracle, SQL Server, and SQLite. The specific syntax and behavior may vary slightly between database systems, but the fundamental functionality remains the same.

Related

SQL MAX

SQL ROUND

SQL POWER

Share

Book a demo

Work email

Start syncing data in minutes

Check out this 5 minute interactive demo to learn how you can sync customer data to any business tool.

Hightouch Audiences user interface.

Activate your data in less than 5 minutes