ABS Function - SQL
Overview
The ABS function in SQL returns the absolute value of a numeric expression. It is useful for ensuring that the result is always non-negative.
Example:
SELECT
ABS(-10)
Syntax:
SELECT
ABS(numeric_expression)
numeric_expression
is the numeric value for which you want to find the absolute value.
Sample Data:
first_name | vacation_days |
---|---|
Frank | 5 |
Jane | 2 |
Ashley | 3 |
Glenn | -3 |
Kelly | 2 |
Richard | -7 |
George | 2 |
Kyle | 1 |
James | -2 |
Gustavo | -10 |
Example: Hard-coded value
In this example, we are returning the absolute value of -10 with the final output being 10.
Example: Query without the WHERE Statement
In this example, we are returning the absolute value of the vacation_days column in the company.employees table.
Example: Query with the WHERE Statement
In this example, we are returning the absolute value of the vacation_days column in the company.employees table where the first_name equals ‘Glenn.’