Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)
  • If you execute SELECT -100/-100*10 the result is 0.
  • If you execute SELECT (-100/-100)*10 the result is 10.
  • If you execute SELECT -100/(-100*10) the result is 0.
  • If you execute SELECT 100/100*10 the result is 10.

BOL  states:

When two operators in an expression have the same operator precedence level, they are evaluated left to right based on their position in the expression.

And

Level   Operators

  1     ~ (Bitwise NOT)

  2     * (Multiplication), / (Division), % (Modulus)

  3     + (Positive), - (Negative), + (Addition), + (Concatenation), - (Subtraction), & (Bitwise AND), ^ (Bitwise Exclusive OR), | (Bitwise OR)

Is BOL wrong, or am I missing something? It seems the - is throwing the (expected) precedence off.

1 Answer

0 votes
by (40.7k points)

According to the precedence table, the operator with higher precedence such as / and * will be evaluated before the operator with lower precedence like unary -, +. 

Hence this:

-100 / -100 * 10

will be evaluated as:

-(100 / -(100 * 10))

Note: This rule is different from most of the programming languages where unary negation has higher precedence than multiplication and division e.g. VB, JavaScript.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...