Previous Topic: DurationsNext Topic: Functions


Precedence of Operations

Expressions within parentheses are evaluated first. When the order of evaluation is not specified by parentheses, operations are done in the following order.

  1. Prefix operators (unary + or unary -)
  2. multiplication and division
  3. Addition and subtraction

Operators at the same level are evaluated from left to right.

The examples that follow show the difference in results when you use parentheses to specify an order of evaluation in an expression. The same equation is used in each example, but parentheses are either omitted or inserted to show that different results are possible.

Example 1: RESULT = 10 + 20 / 5 - 1 * 2

RESULT = 10 + 20 / 5 - 1 * 2

Division is performed since it is the first operation with the highest order of precedence going left to right in the expression.

RESULT = 10 + 4 - 1 * 2

Multiplication is performed next since it has the highest order of precedence among the remaining operations.

RESULT = 10 + 4 - 2

Addition is performed since it is the first operation going from left to right among the remaining operations.

RESULT = 14 - 2

Subtraction is performed since it is the remaining operation.

RESULT = 12

 

Example 2: RESULT = (10 + 20) / 5 - 1 * 2

RESULT = (10 + 20) / 5 - 1 * 2

Addition is performed since it is enclosed in parentheses.

RESULT = 30 / 5 - 1 * 2

Division is performed next since it is the first operation with the highest order of precedence going from left to right in the expression.

RESULT = 6 - 1 * 2

Multiplication is performed next since it has the highest order of precedence among the remaining operations.

RESULT = 6 - 2

Subtraction is performed since it is the remaining operation.

RESULT = 4

 

Example 3: RESULT = ((10 + 20) / 5 - 1) * 2

RESULT = ((10 + 20) / 5 - 1) * 2

Addition is performed since it is enclosed in the inner pair of parentheses.

RESULT = (30 / 5 - 1) * 2

Division is performed since it has a higher order of precedence than subtraction, even though both are enclosed within parentheses.

RESULT = (6 - 1) * 2

Subtraction is performed since it is enclosed in parentheses.

RESULT = 5 * 2

Multiplication is performed since it is the remaining operation.

RESULT = 10

 

Examples

Some examples of expressions are:

Example 1: This example finds the maximum value for each instance of a salary added to a year-to-date commission.

 MAX(SALARY + YTDCOMM)

Example 2: This example finds the number of distinct status codes, eliminating any duplicates.

 COUNT(DISTINCT STATUS)

Example 3: This expression calculates the discount by multiplying the cost by 5 percent.

 DISCOUNT = COST * .05

Example 4: This expression calculates the total amount of all discounts by summing the results for each instance when the cost is multiplied by 5 percent.

 TOTDISC = SUM(COST * .05)