SQL SERVER – Basic Calculation and PEMDAS Order of Operation

After thinking a long time, I have decided to write about this blog post. I had no plan to create a blog post about this subject but the amount of conversation this one has created on my Facebook page, I decided to bring up a few of the question and concerns discussed on the Facebook page. There are more than 10,000 comments here so far.

There are lots of discussion about what should be the answer. Well, as far as I can tell there is a big debate going on on Facebook, for educational purpose you should go ahead and read some of the comments. They are very interesting and for sure teach some new stuff. Even though some of the comments are clearly wrong they have made some good points and I believe it for sure develops some logic. Here is my take on this subject. I believe the answer is 9 as I follow PEMDAS  Order of Operation. PEMDAS stands for  parentheses, exponents, multiplication, division, addition, subtraction. PEMDAS is commonly known as BODMAS in India. BODMAS stands for Brackets, Orders (ie Powers and Square Roots, etc), Division, Multiplication,  Addition and Subtraction. PEMDAS and BODMAS are almost same and both of them follow the operation order from LEFT to RIGHT.

Let us try to simplify above statement using the PEMDAS or BODMAS (whatever you prefer to call).

Step 1: 6 ÷ 2 (1+2) (parentheses first)
Step 2: = 6 ÷ 2 * (1+2) (adding multiplication sign for further clarification)
Step 3: = 6 ÷ 2* (3) (single digit in parentheses – simplify using operator)
Step 4: = 6 ÷ 2 * 3 (Remember next Operation should be LEFT to RIGHT)
Step 5: = 3 * 3 (because 6 ÷ 2 = 3; remember LEFT to RIGHT)
Step 6: = 9 (final answer)

Some often find Step 4 confusing and often ended up multiplying 2 and 3 resulting Step 5 to be 6 ÷ 6, this is incorrect because in this case we did not follow the order of LEFT to RIGHT. When we do not follow the order of operation from LEFT to RIGHT we end up with the answer 1 which is incorrect.

Let us see what SQL Server returns as a result.

I executed following statement in SQL Server Management Studio

SELECT 6/2*(1+2)

It is clear that SQL Server also thinks that the answer should be 9.

Let us go ahead and ask Google what will be the answer of above question in Google I have searched for the following term: 6/2(1+2)

The result also says the answer should be 9.

If you want a further reference here is a great video which describes why the answer should be 9 and not 1.

And here is a fantastic conversation on Google Groups.

Well, now what is your take on this subject? You are welcome to share constructive feedback and your answer may be different from my answer.

NOTE: A healthy conversation about this subject is indeed encouraged but if there is a single bad word or comment is flaming it will be deleted without any notification (it does not matter how valuable information it contains).

Reference: Pinal Dave (http://blog.SQLAuthority.com)

About these ads

18 thoughts on “SQL SERVER – Basic Calculation and PEMDAS Order of Operation

  1. Hi Pinal,

    This is kiran i am new to SQL server and i need to list out all the tables used in stored procedure. collecting manually is very tedious job for such a long procedures so i am searching for alternative smart way. please suggest me how could i.

    Thanks
    Kiran

    Like

  2. You just got trolled hard, so hard that it consumed a lengthy blog post and much distress. Roughly 98% of the people that debate that are clearly trolling for the sake of rustling some jimmies. Congrats Pinal, you may have been their biggest catch with that bait.

    Like

  3. Occam’s Razor applies here:
    “Never assume malice when stupidity is equally applicable”

    It is fairly basic stuff, but I’m not surprised a lot of people don’t remember it.

    Like

  4. I’m slightly confused. PEMDAS: Parentheses [(2+1)], exponents [none], multiplication [2*(2+1)] = [2*3] = [6], division [6 / 2 * (2+1)] = [6 / 6], addition [none], subtraction [none]. 6 / 6 = 1.

    Wouldn’t this mean the difference between PEMDAS and BODMAS is the reason for different results? Thus PEMDAS can’t be commonly known as BODMAS in India. Instead, BODMAS is used instead of PEMDAS in India?

    Like

    • And to reply to myself, searched more of this and found out it is actually PE(MD)(AS) (left to right). Didn’t even remember such a thing. For some reason my mind turns 2(2+1) more important than 2 * 3. Anyway, glad to see this conversation as now I don’t need as much parenthesis to make things work for sure when scripting, I just need to remember the left-to-right rule of PEMDAS. :D

      Like

  5. Hi all. I researched this topic well for about 2 weeks now and have come to the following conclusions. I will summarize what I have said in other forums

    with respect to the notations, then I will address other points.

    First,

    if you want to say 0.5x, then you HAVE to write (1/2)x with parentheses or, x “all over 2″ with a horiztonal fraction bar, or write x/2. I have never

    seen (1/2)x before I researched this equation, but since searching online, I HAVE seen fractional coefficients written this way, only because computers

    are limited to the horizontal typing space.

    Therefore:

    x/2 = (1/2)x = 0.5x

    1/2n = 1/(2n) This sort of notation is used especially with pi, ln, or e. We have never had to say 1/(2pi). It was simply 1/2pi, or 1/2e^2.

    I have always used ab/cd to mean (ab)/(cd) and I topped almost all of my calculus classes since high school through university.(moot point, I know)

    Just to re-iterate, to use 6/2 as a fraction, parentheses are REQUIRED. Every book will tell you this.

    Now consider the Identity Law:

    a = 1a = 1(a)

    We know there is ALWAYS an ‘invisible’ 1 as a ceofficient of a variable if no other number is there. Therefore:

    a/a = 1, and if a is also 1a, then a/1a = 1. Blindly using ‘pemdas’, some folks would do this:

    a/1a = a/1*a = a*a = a^2. I hope this drives home the silliness of this calculation.

    Now, on to my second point:

    consider: factoring, simplifying equations, and the distributive property.

    Lets start with the number 6.

    6 = (4+2). There is a common factor here: 2. So let’s factor it out of both terms.

    (4+2) = 2(2+1). The outside 2 remains a part of of the 2 inner terms at all times. It cannot be used in an operation by itself without the rest of (4+2).

    The reverse of factoring is distribution, so, 2(2+1) = 6. This has to be true always. The argument I have seen to this is that (6/2) can be distributed.

    This is true ONLY is 6/2 is in parentheses, otherwise, the 6 and 2 are separated by a division slash, and the 2 is a factor of 2+1.

    So, let’s prove the initial equation:

    6/6 = 1

    6/(4+2) = 1

    6/2(2+1) = 1

    the same can be done for other factors:

    6/6 = 1

    6/(3+3) = 1

    6/3(1+1) = 1

    Distribution is actually a part of “Simplifying Equations” and is not bound to the order of operations as “multiplication”, since it is in fact “removing

    parentheses by distributing”. This can be googled and several references found.

    Simplifying 2(2+1) + 3(2+1) = 5(2+1). We “combined like terms” here, by adding, and did not perform the “parntheses” part of order of operations, nor did

    we multiply, which is also higher priority than adding, because we only simplified.

    Lastly, I hear the argument that “This is strictly numbers and you don’t use algebra rules since there are no variables”. That is the most asinine

    arguement I have heard yet. All axioms, laws, and properties use variables, meaning that they hold true for “any number”, hence the proofs with

    variables.

    I welcome thoughts on this, in an intellectually formed response. I am tired of the ‘flaming’ that goes on by imbciles on some other forums with

    rebuttals like “it is 9. go back to grade 3 you moron”, or “google says it is 9″, when google changes the equation to (6/2)*(2+1), and wolfram

    contradicts itself with 2n/2n = 1, and 6/2n = 3/n, but then says 6/2(2+1) is 9. wolframs “terms” state that any answer should be verified with common

    sense and accuracy should also be verified.

    Like

  6. The 2 in front of the parenthesis implies possession, doubling the parenthesis is the intent. Otherwise an alternative symbol would have been used to imply multiplication. In which case 9 would be correct.
    6/2*(1 + 2) = 9
    6/2(1+2)= 1

    Like

    • I totally agree.
      This equation is 6 over 2(2+1).
      No one ever wrote a / bc when they meant ac / b
      That is because they are different expressions.
      Distributive property cannot change the final value,
      and, when used, is 6 / (4+2).
      One more thing, do the factoring for 6 + 3 = 9
      Now factor 6/2 from 6 + 3 and we get:
      (6/2)2 + (6/2)1 = 9
      Notice how 6/2 MUST be in parentheses?
      Now final step:
      (6/2)(2+1)
      The same simple idea applies to “Foiling” binomials:
      x+1(x+2) = x + ((1)x + (1)2)
      (x+1)(x+2) = (x+1)x + (x+1)2

      Like

  7. Pingback: SQL SERVER – Weekly Series – Memory Lane – #048 | Journey to SQL Authority with Pinal Dave

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s