Database

MySQL Arithmetic Operators

MySQL Arithmetic Operators

In this tutorial, we will learn about MySQL Arithmetic operators with examples. Arithmetic operators are used to perform mathematical operations like addition, subtraction, etc on the column values. MySQL arithmetic operators are as follows:

 

MySQL Arithmetic Operator Description
+ Addition
– Subtraction
* Multiplication
/ Division
DIV Integer Division
% Modulo – Remainder after integer division

Operator Precedence

The arithmetic operators have the following precedence.

* multiplication, / Division, % Modulo
– Subtraction, + Addition

Multiplication , Division, Modulo have higher precedence over the Subtraction and Addition operators.

Examples

mysql> SELECT 7 + 9;
+——-+
| 7 + 9 |
+——-+
| 16 |
+——-+
1 row in set (0.02 sec)

MySQL Arithmetic Addition Operator

Modulus operator

mysql> SELECT 17 % 3;
+——–+
| 17 % 3 |
+——–+
| 2 |
+——–+
1 row in set (0.00 sec)

Returns the remainder after the integer division.

MySQL Modulus Operator

#

mysql> SELECT 2 * 3 + 7;
+———–+
| 2 * 3 + 7 |
+———–+
| 13 |
+———–+
1 row in set (0.00 sec)

mysql> — The * operator has high precedence over +
mysql> — 2 * 3 + 7 -> 6 + 7 -> 13
mysql> — result = 13
mysql> — If + had higher precedence then the result would
mysql> — have been 20! But, It’s NOT the case

MySQL Arithmetic Operator Precedence

 

MySQL Tutorials

MySQL Tutorials on this website:

https://www.testingdocs.com/mysql-tutorials-for-beginners/