Arithmetic Operations in SQL

Read this Oracle Tutorial Point article to know about arithmetic operations in SQL and how to apply in SELECT statement. The Arithmetic operators will be used to create expressions on NUMBER and DATE data. The Arithmetic Operators supported are,
  • Addition(+)
  • Subtraction(-)
  • Multiply(*)
  • Divide(/)
Arithmetic Operators can be used in any of the SQL statement except the FROM clause. SQL * Plus forget Back spaces(<---) before and after the Arithmetic operator.

Applying Arithmetic Operations in SELECT Statement:
By using SELECT statements, Arithmetic Expressions can be implemented. Arithmetic Expressions can be implemented to,
  • Perform Calculations.
  • Modify the way the data is displayed.
  • Implement WHAT-IF Scenario.
An Arithmetic Expression can contain,
  • Simple Column names.
  • Arithmetic operators.
  • Constant numeric values.
Examples:
SQL> SELECT ENO,ENAME,SAL,SAL+100 FROM EMP;

SQL> SELECT ENO,ENAME,SAL,SAL-500 FROM EMP;

Operator Precedence:
Multiplication(*) and Division(/) take preference over addition(+) and subtraction(-). Operators of the same priority are evaluated from left to right. To increase clarity and to prioritize evaluation parentheses can be implemented.
SQL> SELECT ENO,ENAME,SAL,12 * SAL + 100 FROM EMP;

SQL> SELECT ENO,ENAME,SAL,(12*SAL)+100 FROM EMP;

SQL> SELECT ENO,ENAME,SAL,12*(SAL+100) FROM EMP;

Based on the above SQL queries you try for other Arithmetic operators.

Comments