BBampaa

Lessons › SQL › Deep Dive: Text, Date & Number Functions

SQL lesson

ABS()

ABS() gives you the absolute value of a number - meaning it drops any negative sign and returns just the size, never the direction.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

Measuring Retail Return Differences with ABS()

SELECT return_id, category, price_difference, ABS(price_difference) AS abs_difference
FROM returns
ORDER BY abs_difference DESC

Result:

return_idcategoryprice_differenceabs_difference
R2004Home-130.0130.0
R2001Electronics-45.9945.99
R2002Clothing12.512.5
R2003Beauty-8.758.75
R2005Grocery3.23.2

Step by step

  1. Run the query without ABS so you can see what the unfiltered values look like.
    SELECT return_id, category, price_difference, price_difference
    FROM returns
    ORDER BY abs_difference DESC;
  2. ABS returns the value without its sign - always positive.
    SELECT return_id, category, price_difference, ABS(price_difference)
    FROM returns
    ORDER BY abs_difference DESC;
  3. AS gives the ABS result a friendly column name so the output reads cleanly.
    SELECT return_id, category, price_difference, ABS(price_difference) AS abs_difference
    FROM returns
    ORDER BY abs_difference DESC;

Practice

Try each one in your head first, then open the answer.

1. Using orders, write a query to show the absolute value of revenue (turn negatives into positives).
SELECT ABS(revenue) FROM orders
2. Using store_sales, write a query to show the absolute value of revenue (turn negatives into positives).
SELECT ABS(revenue) FROM store_sales
3. Using products, write a query to show the absolute value of price (turn negatives into positives).
SELECT ABS(price) FROM products
4. Using reviews, write a query to show the absolute value of rating (turn negatives into positives).
SELECT ABS(rating) FROM reviews
5. Using employees, write a query to show the absolute value of salary (turn negatives into positives).
SELECT ABS(salary) FROM employees

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed