MySQL ABS() Function: Find the absolute value

ABS() returns the magnitude of a number without its negative sign. It is useful for measuring the size of a difference between stored values.

ABS(): Quick SQL Example

Run this SELECT query directly in MySQL or phpMyAdmin. It uses fixed inputs so you can verify the result without creating a table.

SELECT ABS(-12.5) AS absolute_value;
absolute_value
12.5
On this page — Show page sectionsHide page sections

More SQL Examples and Explanations

We can get absolute value by using abs function. This function takes one numeric input data. Here is an example
SELECT ABS(-2.3);
Output of above code is 2.3
SELECT ABS(4.567);
Output is 4.567
SELECT ABS(0.23);
It is 0.23

Let us try ABS Query in a table where we have two columns for each product storing Purchased ( buying ) price and selling price. We will first list the difference in price like this
SELECT product, buy_price, sell_price,
       sell_price - buy_price AS difference
FROM plus2_price;
Output is here
productbuy_pricesell_pricedifference
Product110155
Product12015-5
Product110100
Product120255
Now let us apply ABS function by modifying the query.
SELECT product, buy_price, sell_price,
       sell_price - buy_price AS difference,
       ABS(sell_price - buy_price) AS ABS_difference
FROM plus2_price;
productbuy_pricesell_pricedifferenceABS_
difference
Product1101555
Product12015-55
Product1101000
Product1202555

ABS With WHERE Query

SELECT product, buy_price, sell_price,
       sell_price - buy_price AS difference,
       ABS(sell_price - buy_price) AS ABS_difference
FROM plus2_price
WHERE ABS(sell_price - buy_price) > 0;

More about SQL Where condition Query

The SQL Dump of this table is here
CREATE TABLE IF NOT EXISTS plus2_price (
  product VARCHAR(10) NOT NULL,
  buy_price INT NOT NULL,
  sell_price INT NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO plus2_price (product, buy_price, sell_price) VALUES
('Product1', 10, 15),
('Product1', 20, 15),
('Product1', 10, 10),
('Product1', 20, 25);

How to Interpret the Result

ABS() removes the sign, so a loss and a gain of the same amount have the same magnitude. Use SIGN() if direction matters.

Common Question

Does ABS() change a positive value?
No. ABS(12.5) is 12.5; ABS(0) is 0.

Further Reading

All Math Functions ROUND()

Related: SIGN()

More related tutorials: All math functions

↑ Back to top




Subscribe to our YouTube Channel here



plus2net.com




SQL Video Tutorials










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer