Arithmetic Operators

Arithmetic operators in SQL are used to perform mathematical operations in queries. They can be used in SELECT statements, WHERE clauses, and in many other parts of SQL statements.

List of Arithmetic Operators

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo (remainder of division)

Examples

Addition (+)

SELECT product_name, price, price + 10 AS increased_price 
FROM products;

This query shows the original price and a price increased by 10 for each product.

Subtraction (-)

SELECT order_id, total_amount, paid_amount, 
       total_amount - paid_amount AS remaining_balance
FROM orders;

This query calculates the remaining balance for each order.

Multiplication (*)

SELECT product_name, quantity, price, 
       quantity * price AS total_value
FROM order_details;

This query calculates the total value of each order detail.

Division (/)

SELECT first_name, last_name, salary, 
       salary / 12 AS monthly_salary
FROM employees;

This query calculates the monthly salary for each employee.

Modulo (%)

SELECT order_id, order_id % 2 AS odd_even_indicator
FROM orders;

This query uses the modulo operator to determine if an order ID is odd or even.

Combining Operators

SELECT product_name, price, 
       ROUND((price * 1.1), 2) AS price_with_tax
FROM products;

This query calculates a price with 10% tax, rounded to 2 decimal places.

Note that the exact behavior of arithmetic operators, especially division and modulo, can vary slightly between different database systems. Always check your specific database's documentation for precise details.