MYSQL

The MySQL ABS() function is a mathematical function that returns the absolute value of a given number or numeric expression.

MySQL abs function


➔ The ABS() function returns the absolute value of an expression or a table column for all records used in the function.

➔ This is useful in situations where only magnitude is required, regardless of whether it is negative or positive.

Syntax
/* MySQL abs() function syntax */
ABS(X)

➔ X: X is a numerical value or an expression.

➔ If the input X is NULL, the function returns NULL.

Example

/* The result is 52 because the value is already non-negative.*/
SELECT ABS(52);

Copy the code and try it out practically in your learning environment.


Example

/* The result is 32*/
SELECT ABS(-32);

Copy the code and try it out practically in your learning environment.


Example

/* The result is 57*/
SELECT ABS(10 + 35 - 102);

Copy the code and try it out practically in your learning environment.


Example

/* The price column is used in abs function */
SELECT product_name, ABS(price) AS positive_price
FROM products;

Copy the code and try it out practically in your learning environment.