Arithmetic operators are used to perform basic mathematical operations on numeric data stored in tables. These operators can be applied to single columns or combinations of columns to compute new values.
List of Arithmetic Operators:
Operator | Description |
---|---|
+ | Addition |
- | Subtraction |
/ | Division |
* | Multiplication |
% | Modulus (remainder) |
Examples of SQL Arithmetic Operations:
- Addition (
+
):
- Single Column: Adds a constant value (e.g., 100) to a column.
SELECT employee_id, employee_name, salary, salary + 100 AS "salary + 100"
FROM addition;
Output:
employee_id | employee_name | salary | salary + 100 |
---|---|---|---|
1 | alex | 25000 | 25100 |
2 | rr | 55000 | 55100 |
3 | jpm | 52000 | 52100 |
4 | ggshmr | 12312 | 12412 |
- Addition of Two Columns:
SELECT employee_id, employee_name, salary, salary + employee_id AS "salary + employee_id"
FROM addition;
Output:
employee_id | employee_name | salary | salary + employee_id |
---|---|---|---|
1 | alex | 25000 | 25001 |
2 | rr | 55000 | 55002 |
3 | jpm | 52000 | 52003 |
4 | ggshmr | 12312 | 12316 |
- Subtraction (
-
):
- Single Column: Subtracts a constant value (e.g., 100) from a column.
SELECT employee_id, employee_name, salary, salary - 100 AS "salary - 100"
FROM subtraction;
Output:
employee_id | employee_name | salary | salary - 100 |
---|---|---|---|
12 | Finch | 15000 | 14900 |
22 | Peter | 25000 | 24900 |
32 | Warner | 5600 | 5500 |
42 | Watson | 90000 | 89900 |
- Subtraction of Two Columns:
SELECT employee_id, employee_name, salary, salary - employee_id AS "salary - employee_id"
FROM subtraction;
Output:
employee_id | employee_name | salary | salary - employee_id |
---|---|---|---|
12 | Finch | 15000 | 14988 |
22 | Peter | 25000 | 24978 |
32 | Warner | 5600 | 5568 |
42 | Watson | 90000 | 89958 |
- Multiplication (
*
):
- Single Column: Multiplies a column by a constant value (e.g., 100).
SELECT employee_id, employee_name, salary, salary * 100 AS "salary * 100"
FROM addition;
Output:
employee_id | employee_name | salary | salary * 100 |
---|---|---|---|
1 | Finch | 25000 | 2500000 |
2 | Peter | 55000 | 5500000 |
3 | Warner | 52000 | 5200000 |
4 | Watson | 12312 | 1231200 |
- Multiplication of Two Columns:
SELECT employee_id, employee_name, salary, salary * employee_id AS "salary * employee_id"
FROM addition;
Output:
employee_id | employee_name | salary | salary * employee_id |
---|---|---|---|
1 | Finch | 25000 | 25000 |
2 | Peter | 55000 | 110000 |
3 | Warner | 52000 | 156000 |
4 | Watson | 12312 | 49248 |
- Modulus (
%
):
- Single Column: Returns the remainder when one column value is divided by another (e.g.,
salary % 25000
).
SELECT employee_id, employee_name, salary, salary % 25000 AS "salary % 25000"
FROM addition;
Output:
employee_id | employee_name | salary | salary % 25000 |
---|---|---|---|
1 | Finch | 25000 | 0 |
2 | Peter | 55000 | 5000 |
3 | Warner | 52000 | 2000 |
4 | Watson | 12312 | 12312 |
Key Points:
- Addition (
+
): Adds values in a column or between columns. - Subtraction (
-
): Subtracts values in a column or between columns. - Multiplication (
*
): Multiplies values in a column or between columns. - Division (
/
): Divides one value by another (not covered in the examples above). - Modulus (
%
): Returns the remainder of division between values.
Arithmetic operations are commonly used in SQL queries to perform calculations directly within SELECT statements and manipulate data efficiently.