Syntax with Example
MySQL, one of the most popular relational database management systems, offers a wide array of functionalities to manipulate and retrieve data. Let’s explore the various mathematical operators available in MySQL with detailed examples and outputs:
1. Addition (+)
The addition operator (+) in MySQL is used to add numeric values together. Let’s consider a table named numbers with two columns: num1 and num2.
CREATE TABLE numbers (
num1 INT,
num2 INT
);
INSERT INTO numbers (num1, num2) VALUES (5, 10), (15, 20);
Output:
Now, let’s retrieve the sum of num1 and num2 using the addition operator:
SELECT num1, num2, num1 + num2 AS sum FROM numbers;
Output:
2. Subtraction (-)
The subtraction operator (-) in MySQL is used to subtract one numeric value from another.
SELECT num1, num2, num1 - num2 AS difference FROM numbers;
Output:
3. Multiplication (*)
The multiplication operator (*) in MySQL is used to multiply numeric values together
SELECT num1, num2, num1 * num2 AS product FROM numbers;
Output:
4. Division (/)
The division operator (/) in MySQL is used to divide one numeric value by another.
SELECT num1, num2, num1 / num2 AS quotient FROM numbers;
Output:
5. Modulus (%)
The modulus operator (%) in MySQL returns the remainder of dividing one numeric value by another.
SELECT num1, num2, num1 % num2 AS remainder FROM numbers;
Output:
6. Exponentiation (POWER())
Although MySQL doesn’t have a built-in exponentiation operator, you can achieve exponentiation using the POWER() function.
SELECT num1, num2, POWER(num1, num2) AS exponent FROM numbers;
Output:
These examples illustrate how to use each mathematical operator in MySQL along with their respective syntax and outputs. By incorporating these operators into your SQL queries, you can perform a wide range of arithmetic operations on numeric data stored in your database tables.
Arithmetic Operators in MySQL
Arithmetic operators in MySQL can work with numeric operands and hence carry out mathematic operations within MySQL queries. MySQL supports only a small set of arithmetic operators (+, –, *, /, and %) which is equivalent to the Mainstream calculator operations.
Apart from these conventional functions of arithmetic operators, MySQL also has a set of customary procedures. Besides these Operators and Functions are necessary in the MySQL databases for the numerical calculation and sorting of data. In this post, we are going to read and discover the arithmetic instruction signs.
Contact Us