Arithmetic Operators in SQL
? Arithmetic Operators in SQL
Arithmetic operators in SQL are used to perform mathematical operations on numeric data types like INT, FLOAT, DECIMAL, etc.
? List of Arithmetic Operators
| Operator | Description | Example |
|---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (remainder) | a % b |
? Example Table: products
| id | name | price | quantity |
|---|---|---|---|
| 1 | Laptop | 50000 | 2 |
| 2 | Keyboard | 1500 | 5 |
? Example Queries
? 1. Addition (+)
SELECT name, price + 500 AS increased_priceFROM products;? 2. Subtraction (-)
SELECT name, price - 1000 AS discount_priceFROM products;? 3. Multiplication (*)
SELECT name, price * quantity AS total_valueFROM products;? 4. Division (/)
SELECT name, price / quantity AS unit_priceFROM products;? 5. Modulus (%)
SELECT name, price % 1000 AS price_remainderFROM products;? Can Be Used In:
SELECT(to create calculated columns)WHERE(for filtering with expressions)UPDATE(to update values using math)
? Example in UPDATE
UPDATE productsSET price = price * 1.10WHERE name = 'Laptop';? Notes
Be careful of division by zero.
Data types affect results (e.g., integer division truncates in some databases).
You can use aliases (
AS) to name calculated columns.
Let me know if you want examples using CASE, conditions, or with GROUP BY!