The SQL ABS() function is commonly used in financial calculations, distance calculations, scientific applications and data analysis where only the magnitude of a number is required regardless of whether it is positive or negative.
This function in SQL is a mathematical function that returns the absolute (positive) value of a number. If the given number is negative then the function removes the negative sign and returns the positive value. If the number is already positive or zero then it returns the same value without making any changes.
For example:
This function works with integers, decimal values, floating-point numbers and values returned by mathematical expressions.
Parameters:
number: It is the numeric value whose absolute value needs to be returned.
Return Value:
The ABS() function returns the absolute value of the given numeric expression.
Let us create a table named EmployeeScores that stores the information about the employees and contains the following fields:
ID: It stores the unique identification number of each employee.
Name: It stores the employee name.
Department: It stores the department in which the employee works.
ScoreDifference: It stores the difference in performance score. Positive values indicate improvement, while negative values indicate a decrease.
SalaryChange: It stores the amount by which the employee's salary changed. Positive values represent salary increments, whereas negative values represent salary deductions.
SQL Query:
Now we will add some values to the table.
SQL Query:
Initial table:
Output:
| ID | Name | Department | ScoreDifference | SalaryChange |
|---|---|---|---|---|
| 101 | Emma | Sales | 25 | 1200.50 |
| 102 | Liam | Finance | -18 | -850.75 |
| 103 | Noah | HR | 32 | 1500.00 |
| 104 | Olivia | Marketing | -40 | -2200.25 |
| 105 | Ava | IT | 15 | 950.00 |
| 106 | Mason | Support | -27 | -1350.60 |
| 107 | Sophia | Admin | 0 | 0.00 |
| 108 | Lucas | Operations | 48 | 2750.40 |
| 109 | Mia | Research | -55 | -3100.90 |
Here we will learn how to use the ABS() function in SQL to convert negative score
Here we will learn how to use the ABS() function in SQL to convert negative score differences into positive values. This example helps us understand how the SQL ABS() function simplifies mathematical calculations and makes performance comparisons easier during SQL data analysis.
SQL Query:
Output:
| ID | Name | ScoreDifference | AbsoluteScoreDifference |
|---|---|---|---|
| 101 | Emma | 25 | 25 |
| 102 | Liam | -18 | 18 |
| 103 | Noah | 32 | 32 |
| 104 | Olivia | -40 | 40 |
| 105 | Ava | 15 | 15 |
| 106 | Mason | -27 | 27 |
| 107 | Sophia | 0 | 0 |
| 108 | Lucas | 48 | 48 |
| 109 | Mia | -55 | 55 |
Explanation:
The output shows that every negative score difference has been converted into a positive value while positive values and zero remain unchanged. It makes it easier to compare the size of score changes without considering their direction.
Here we will learn how to utilize the ABS() function in SQL to display the absolute value of salary changes.
SQL Query:
Output:
| Name | SalaryChange | AbsoluteSalaryChange |
|---|---|---|
| Emma | 1200.50 | 1200.50 |
| Liam | -850.75 | 850.75 |
| Noah | 1500.00 | 1500.00 |
| Olivia | -2200.25 | 2200.25 |
| Ava | 950.00 | 950.00 |
| Mason | -1350.60 | 1350.60 |
| Sophia | 0.00 | 0.00 |
| Lucas | 2750.40 | 2750.40 |
| Mia | -3100.90 | 3100.90 |
Explanation:
The output removes the negative sign from salary deductions and displays every salary change as a positive amount. It assists users easily compare the magnitude of salary changes regardless of whether they were increases or decreases.
Here we will learn how to use the ABS() function in SQL with a mathematical expression instead of a single column value.
SQL Query:
Output:
| Name | ScoreDifference | DifferenceFromTwenty |
|---|---|---|
| Emma | 25 | 5 |
| Liam | -18 | 38 |
| Noah | 32 | 12 |
| Olivia | -40 | 60 |
| Ava | 15 | 5 |
| Mason | -27 | 47 |
| Sophia | 0 | 20 |
| Lucas | 48 | 28 |
| Mia | -55 | 75 |
Explanation:
The output calculates the difference between each score and 20 before applying the ABS() function. The final result always shows a positive distance from 20 which makes comparisons much easier.
Here we will learn how to use the ABS() function in SQL together with the ORDER BY clause.
SQL Query:
Output:
| Name | ScoreDifference | AbsoluteScore |
|---|---|---|
| Mia | -55 | 55 |
| Lucas | 48 | 48 |
| Olivia | -40 | 40 |
| Noah | 32 | 32 |
| Mason | -27 | 27 |
| Emma | 25 | 25 |
| Liam | -18 | 18 |
| Ava | 15 | 15 |
| Sophia | 0 | 0 |
Explanation:
The output sorts the employees according to the largest absolute score difference instead of the actual positive or negative value. It makes it easy to identify employees with the biggest overall changes irrespective of the direction of those changes.
In this example, we will use the ABS() function with a constant negative value. It helps us understand how the function works without using any table data.
SQL Query:
Output:
| AbsoluteValue |
|---|
| 500 |
Explanation:
The ABS() function removes the negative sign from -500 and returns 500. Since the purpose of the function is to return the absolute value so the result is always non-negative.
In this example, we will calculate the employees whose Salary Change is greater than $1000 using the WHERE clause.
SQL Query:
Output:
| Name | SalaryChange | AbsoluteSalary |
|---|---|---|
| Emma | 1200.50 | 1200.50 |
| Noah | 1500.00 | 1500.00 |
| Olivia | -2200.25 | 2200.25 |
| Mason | -1350.60 | 1350.60 |
| Lucas | 2750.40 | 2750.40 |
| Mia | -3100.90 | 3100.90 |
| Emma | 1200.50 | 1200.50 |
| Noah | 1500.00 | 1500.00 |
| Olivia | -2200.25 | 2200.25 |
Explanation:
The ABS() function converts all salary changes into positive values before the comparison is made. Only employees whose salary change exceeds 1000 in magnitude are displayed.
In this example, we will combine the ABS() function with the SUM() aggregate function to calculate the total magnitude of score changes across all employees.
SQL Query:
Output:
| TotalAbsoluteScoreDifference |
|---|
| 260 |
Explanation:
The ABS() function first converts all score differences into positive values. After that, the SUM() function adds them together. It provides the total magnitude of score changes without considering whether the scores increased or decreased.
We will find the absolute value of salary changes and round them upward to the nearest integer using the CEIL() function.
SQL Query:
Output:
| Name | SalaryChange | RoundedUpCharge |
|---|---|---|
| Emma | 1200.50 | 1201 |
| Liam | -850.75 | 851 |
| Noah | 1500.00 | 1500 |
| Olivia | -2200.25 | 2201 |
| Ava | 950.00 | 950 |
| Mason | -1350.60 | 1351 |
| Sophia | 0.00 | 0 |
| Lucas | 2750.40 | 2751 |
| Mia | -3100.90 | 3101 |
Explanation:
The ABS() function converts negative salary changes into positive values and the CEIL() function rounds each value up to the next whole number.
Here we will display only those departments where the average score difference exceeds 20 when the sign of the values is ignored.
SQL Query:
Output:
| Department | AvgScoreDifference |
|---|---|
| Sales | 25.0000 |
| HR | 32.0000 |
| Marketing | 40.0000 |
| Support | 27.0000 |
| Operations | 48.0000 |
| Research | 55.0000 |
Explanation:
The ABS() function converts all score differences into positive values before calculating the average. The HAVING clause then filters departments whose average score difference is greater than 20.
We request you to subscribe our newsletter for upcoming updates.