Arithmetic Operators
In this page:
Addition Operator (+)
The addition operator joins two numbers to find their total sum, such as combining a base price and a shipping fee. It is a fundamental operator in SQL math and works the same way inside SELECT, WHERE, or UPDATE expressions.
Example: Addition Operator (+)
SELECT base_price + shipping_fee AS total FROM orders;
Subtraction Operator (-)
The subtraction operator deducts one number from another. You can use it to find the difference between two values, such as calculating days between a due date and today's date once combined with date functions.
Example: Subtraction Operator (-)
SELECT price - discount AS final_price FROM orders;
Multiplication Operator (*)
The multiplication operator multiplies two numbers together. This is useful for scaling values or calculating totals, like multiplying unit price by quantity to get a line-item subtotal.
Example: Multiplication Operator (*)
SELECT unit_price * quantity AS total FROM order_items;
Division Operator (/)
The division operator divides the first number by the second. It always returns a float result in MySQL, even when dividing two integers evenly, so remember to round or cast if you need a whole number.
Example: Division Operator (/)
SELECT 10 / 4 AS result; -- always returns a float, even for exact division
Modulo Operator (%)
The modulo operator returns the remainder of a division. It is helpful for finding odd or even numbers, or for tasks like distributing rows evenly across a fixed number of buckets.
Example: Modulo Operator (%)
SELECT id, id % 2 AS remainder FROM users; -- 0 = even, 1 = odd
Chapter Quiz — Complete all 5 topics to unlock
0/5 topics done
Complete these topics first: