Using Arithmetic Operators

Previous
Previous
Next
Next

You can use arithmetic operators to create expressions for calculations on data in tables. The arithmetic operators include:

In an arithmetic expression, multiplication and division are evaluated first, then addition and subtraction. When operators have equal precedence, the expression is evaluated left to right. It is best to include parentheses to explicitly determine the order of operators and provide clarity in the expression.

Example: Using SQL Arithmetic Operators shows the use of arithmetic operators in expressions with the data in the employees table. Note the use of a column alias to provide a more descriptive heading for the displayed output.

Using SQL Arithmetic Operators

-- in the following query the commission is displayed as a percentate instead 
-- of the decimal that is stored in the database
SELECT employee_id, (commission_pct * 100) "Commission %" FROM employees;

-- in the following query, the proposed new annual salary is calculated
-- for employees who report to the manager with ID 145
SELECT employee_id, ((salary + 100) * 12) "Proposed new annual salary" 
  FROM employees WHERE manager_id = 145;