Arithmetic Operators

Accessing and Changing Relational Data

Accessing and Changing Relational Data

Arithmetic Operators

Arithmetic operators can be used for any arithmetic computations, such as:

  • Addition.

  • Subtraction.

  • Multiplication.

  • Division.

  • Modulo (the remainder from a division operation).

Here is some information about arithmetic operators:

  • When there is more than one arithmetic operator in an expression, multiplication, division, and modulo are calculated first, followed by subtraction and addition.

  • When all arithmetic operators in an expression have the same level of precedence, the order of execution is left to right.

  • Expressions within parentheses take precedence over all other operations.

The following SELECT statement subtracts the part of the year-to-date sales that the author receives (sales * author's royalty percentage / 100) from the total sales. The result is the amount of money the publisher receives. The product of ytd_sales and royalty is calculated first because the operator is multiplication. Next, the total is divided by 100. The result is subtracted from ytd_sales.

USE pubs
SELECT title_id, ytd_sales - ytd_sales * royalty / 100
FROM titles

For clarity, you can use parentheses:

USE pubs
SELECT title_id, ytd_sales - ((ytd_sales * royalty) / 100)
FROM titles

You can also use parentheses to change the order of execution. Calculations inside parentheses are evaluated first. If parentheses are nested, the most deeply nested calculation has precedence. For example, the result and meaning of the preceding query can be changed if you use parentheses to force the evaluation of subtraction before multiplication:

USE pubs
SELECT title_id, (ytd_sales - ytd_sales) * royalty / 100
FROM titles

See Also

- (Subtract)

+ (Add)

* (Multiply)

/ (Divide)