How to find absolute difference between two numbers MYSQL

mysql

Solution

As simple as that:

SELECT ABS(numberA - numberB) AS spread 
FROM table 
ORDER BY spread DESC

Or, if you want to select the pair (numberA, numberB) in descending order of their difference:

SELECT numberA, numberB
FROM table 
ORDER BY ABS(numberA - numberB) DESC

Problem

What's the best way to find the absolute difference between two numbers in MYSQL so that I may order results? The below works, only if numberA is larger than numberB, but as you can see this is not always the case. Is there a good way to do this with one statement? ``` SELECT (numberA - numberB) AS spread FROM table ORDER BY spread DESC |-------------------| | numberA | numberB | | 5.4 | 2.2 | | 7.7 | 4.3 | | 1 | 6.5 | | 2.3 | 10.8 | | 4.5 | 4.5 | ```

Original source