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)
Ouput 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
product
buy_price
sell_price
difference
Product1
10
15
5
Product1
20
15
-5
Product1
10
10
0
Product1
20
25
5
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
product
buy_price
sell_price
difference
ABS_ difference
Product1
10
15
5
5
Product1
20
15
-5
5
Product1
10
10
0
0
Product1
20
25
5
5
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