MySQL numeric functions
MySQL string functions
MySQL Date/Time functions
MySQL aggregate functions
MySQL flow control functions
MySQL ABS (Absolute Value) function is a mathematical function that returns the absolute (positive) value of a given number. The ABS function can be used with integer, float, or decimal values.
Here's a tutorial on using the MySQL ABS function:
1. Syntax
The syntax for the ABS function is:
ABS(number)
Where number
is the value you want to find the absolute value for.
2. Examples
Let's start with some basic examples of the ABS function.
a. Using ABS with a positive number
SELECT ABS(5);
Result:
5
b. Using ABS with a negative number
SELECT ABS(-7);
Result:
7
c. Using ABS with a floating-point number
SELECT ABS(-3.14);
Result:
3.14
3. Using ABS function in a table
Let's assume we have the following orders
table:
+----+--------+--------+--------+ | id | price | profit | user_id| +----+--------+--------+--------+ | 1 | 10.0 | -2.00 | 1 | | 2 | -20.0 | 10.00 | 2 | | 3 | 15.0 | -4.00 | 1 | | 4 | 30.0 | 5.00 | 3 | +----+--------+--------+--------+
a. Select all records with absolute values for price and profit
SELECT id, ABS(price) AS abs_price, ABS(profit) AS abs_profit, user_id FROM orders;
Result:
+----+-----------+-----------+--------+ | id | abs_price | abs_profit| user_id| +----+-----------+-----------+--------+ | 1 | 10.0 | 2.00 | 1 | | 2 | 20.0 | 10.00 | 2 | | 3 | 15.0 | 4.00 | 1 | | 4 | 30.0 | 5.00 | 3 | +----+-----------+-----------+--------+
b. Calculate the total absolute profit per user
SELECT user_id, SUM(ABS(profit)) AS total_abs_profit FROM orders GROUP BY user_id;
Result:
+--------+----------------+ | user_id| total_abs_profit| +--------+----------------+ | 1 | 6.00 | | 2 | 10.00 | | 3 | 5.00 | +--------+----------------+
In this tutorial, you learned how to use the MySQL ABS function to calculate the absolute value of numbers, both for individual values and within table data. This function can be helpful when you want to analyze data without considering its sign.
How to use ABS function in MySQL:
SELECT ABS(-5) AS absolute_value;
MySQL ABS function examples:
SELECT ABS(-10) AS result1, ABS(5.75) AS result2;
Finding absolute value in MySQL:
SELECT ABS(column_name) FROM table_name;
ABS function in MySQL with negative numbers:
SELECT ABS(-8) AS absolute_value;
Absolute value calculation in MySQL:
SELECT ABS(column1 - column2) AS absolute_difference FROM table_name;
ABS function in MySQL for integers and decimals:
SELECT ABS(15) AS abs_integer, ABS(7.25) AS abs_decimal;
Use cases for ABS function in MySQL queries:
SELECT * FROM products WHERE ABS(price - target_price) < 5;
ABS function in MySQL for positive and negative numbers:
SELECT ABS(-3) AS positive, ABS(8) AS unchanged;
Mathematical operations with ABS in MySQL:
SELECT ABS(column1 + column2) AS sum_absolute FROM table_name;
MySQL ABS function in WHERE clause:
SELECT * FROM data WHERE ABS(column_name) > 10;
ABS function in MySQL for float and double values:
SELECT ABS(-15.75) AS abs_float, ABS(-8.625) AS abs_double;