SQL SERVER – Basic Calculation and PEMDAS Order of Operation
Posted
by pinaldave
on SQL Authority
See other posts from SQL Authority
or by pinaldave
Published on Tue, 25 Sep 2012 01:30:31 +0000
Indexed on
2012/09/25
3:42 UTC
Read the original article
Hit count: 461
About Me
|Technology
|SQL Server
|sql
|SQL Query
|SQL Tips and Tricks
|SQL Authority
|T SQL
|PostADay
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)
Filed under: About Me, PostADay, SQL, SQL Authority, SQL Query, SQL Server, SQL Tips and Tricks, T SQL, Technology
© SQL Authority or respective owner