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:

OperatorDescription
+Addition
-Subtraction
/Division
*Multiplication
%Modulus (remainder)

Examples of SQL Arithmetic Operations:

  1. 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_idemployee_namesalarysalary + 100
1alex2500025100
2rr5500055100
3jpm5200052100
4ggshmr1231212412
  • Addition of Two Columns:
SELECT employee_id, employee_name, salary, salary + employee_id AS "salary + employee_id"
FROM addition;

Output:

employee_idemployee_namesalarysalary + employee_id
1alex2500025001
2rr5500055002
3jpm5200052003
4ggshmr1231212316

  1. 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_idemployee_namesalarysalary - 100
12Finch1500014900
22Peter2500024900
32Warner56005500
42Watson9000089900
  • Subtraction of Two Columns:
SELECT employee_id, employee_name, salary, salary - employee_id AS "salary - employee_id"
FROM subtraction;

Output:

employee_idemployee_namesalarysalary - employee_id
12Finch1500014988
22Peter2500024978
32Warner56005568
42Watson9000089958

  1. 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_idemployee_namesalarysalary * 100
1Finch250002500000
2Peter550005500000
3Warner520005200000
4Watson123121231200
  • Multiplication of Two Columns:
SELECT employee_id, employee_name, salary, salary * employee_id AS "salary * employee_id"
FROM addition;

Output:

employee_idemployee_namesalarysalary * employee_id
1Finch2500025000
2Peter55000110000
3Warner52000156000
4Watson1231249248

  1. 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_idemployee_namesalarysalary % 25000
1Finch250000
2Peter550005000
3Warner520002000
4Watson1231212312

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.