SQL | Arithmetic Operators - GeeksforGeeks

21 Mar, 2018
Prerequisite: Basic Select statement, Insert into clause, Sql Create Clause, SQL Aliases
We can use various Arithmetic Operators on the data stored in the tables.
Arithmetic Operators are:
+           [Addition]
-           [Subtraction]
/           [Division]
*           [Multiplication]
%           [Modulus]
It is used to perform addition operation on the data items, items include either single column or multiple columns.
Implementation:
SELECT employee_id, employee_name, salary, salary + 100
   AS "salary + 100" FROM addition;

Output:
Here we have done addition of 100 to each Employee’s salary i.e, addition operation on single column.
Let’s perform addition of 2 columns:
SELECT employee_id, employee_name, salary, salary + employee_id
   AS "salary + employee_id" FROM addition;

Output:
Here we have done addition of 2 columns with each other i.e, each employee’s employee_id is added with its salary.
It is use to perform subtraction operation on the data items, items include either single column or multiple columns.
Implementation:
SELECT employee_id, employee_name, salary, salary - 100
    AS "salary - 100" FROM subtraction;

Output:
Here we have done subtraction of 100 to each Employee’s salary i.e, subtraction operation on single column.
Let’s perform subtraction of 2 columns:
SELECT employee_id, employee_name, salary, salary - employee_id
    AS "salary - employee_id" FROM subtraction;

Output:
Here we have done subtraction of 2 columns with each other i.e, each employee’s employee_id is subtracted from its salary.
Division (/) : For Division refer this link- Division in SQL
It is use to perform multiplication of data items.
Implementation:
SELECT employee_id, employee_name, salary, salary * 100
    AS "salary * 100" FROM addition;

Output:
Here we have done multiplication of 100 to each Employee’s salary i.e, multiplication operation on single column.
Let’s perform multiplication of 2 columns:
SELECT employee_id, employee_name, salary, salary * employee_id
     AS "salary * employee_id" FROM addition;

Output:
Here we have done multiplication of 2 columns with each other i.e, each employee’s employee_id is multiplied with its salary.
It is use to get remainder when one data is divided by another.
Implementation:
SELECT employee_id, employee_name, salary, salary % 25000
    AS "salary % 25000" FROM addition;

Output:
Here we have done modulus of 100 to each Employee’s salary i.e, modulus operation on single column.
Let’s perform modulus operation between 2 columns:
SELECT employee_id, employee_name, salary, salary % employee_id
    AS "salary % employee_id" FROM addition;

Output:
Here we have done modulus of 2 columns with each other i.e, each employee’s salary is divided with its id and corresponding remainder is shown.
Basically, modulus is use to check whether a number is Even or Odd. Suppose a given number if divided by 2 and gives 1 as remainder, then it is an odd number or if on dividing by 2 and gives 0 as remainder, then it is an even number.
If we perform any arithmetic operation on NULL, then answer is always null.
Implementation:
SELECT employee_id, employee_name, salary, type, type + 100
    AS "type+100" FROM addition;

Output:
Here output always came null, since performing any operation on null will always result in a null value.
Note: Make sure that NULL is unavailable, unassigned, unknown. Null is not same as blank space or zero.To get in depth understanding of NULL, refer THIS link.References: Oracle Docs
SQL-Clauses-Operators
DBMS
SQL
DBMS
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between Clustered and Non-clustered index
SQL | Views
CTE in SQL
Difference between DELETE, DROP and TRUNCATE
Third Normal Form (3NF)
How to find Nth highest salary from a table
SQL | ALTER (RENAME)
SQL | Views
CTE in SQL
Difference between DELETE, DROP and TRUNCATE