The order of operations in SQL Server is the one you learned in school, so 6/2*(1+2) returns 9. Parentheses go first, then multiplication and division, then addition and subtraction. Equal levels run from left to right.

What PEMDAS Says
PEMDAS stands for parentheses, exponents, multiplication, division, addition and subtraction. The same idea is called BODMAS in many countries. The letters make it look like six steps, but there are only four levels. Multiplication and division share one level. Addition and subtraction share another.
A tie inside one level is broken from left to right. That one rule decides 6/2*(1+2). The parentheses give 3, so the expression becomes 6/2*3. Division comes first because it sits on the left, which gives 3*3, and the answer is 9. Multiplying 2 by 3 first would give 6/6 and the answer 1, but that skips the left to right rule.
SQL Server follows the same order of operations. The query below runs the expression and four variations. The last one is a longer chain of mixed operators, which is where mistakes start.
SELECT 6/2*(1+2) AS Original,
6/(2*(1+2)) AS DivideByWholeProduct,
(6/2)*(1+2) AS ExplicitLeft,
6/2*1+2 AS MixedLevels,
3+8-4+5*9/3-8/2*1-2+5 AS LongChain;| Original | DivideByWholeProduct | ExplicitLeft | MixedLevels | LongChain |
|---|---|---|---|---|
| 9 | 1 | 9 | 5 | 21 |
In the long chain, 5*9/3 becomes 15 and 8/2*1 becomes 4. The rest is addition and subtraction from left to right, and the total is 21.
T-SQL Has No Implied Multiplication
The internet argument about 6/2(1+2) is about writing a number next to a parenthesis. Some people read that as multiplication with a higher priority. T-SQL avoids the whole debate, because it doesn’t accept the notation.
SELECT 6/2(1+2);
Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '1'.
In T-SQL, you write the operator you mean. If you want 6 divided by the whole product, write 6/(2*(1+2)). That returns 1, as the table above shows. A formula in a query has one reading, and the parentheses are how you state it.
The T-SQL Order of Operations in Full
The order of operations covers more than arithmetic. A query also mixes bitwise operators, comparisons and logic. The documented order runs from the strongest level to the weakest. Operators on one level are evaluated from left to right.
- Level 1: the bitwise NOT operator (~).
- Level 2: multiplication, division and modulo (*, / and %).
- Level 3: addition, subtraction, string concatenation and the bitwise operators &, ^ and |.
- Level 4: the comparison operators such as = and >.
- Level 5 to 7: NOT, then AND, then OR (with BETWEEN, IN and LIKE sharing the OR level).
Two surprises follow from that list. The bitwise operators sit on the addition level, not above it. In the next query, 1 | 2 * 3 multiplies first and returns 7. The expression 5 + 3 & 4 works from left to right and returns 0.
SELECT 1 | 2 * 3 AS BitOrAfterMultiply,
(1 | 2) * 3 AS BitOrGrouped,
5 + 3 & 4 AS PlusThenAnd,
5 + (3 & 4) AS AndThenPlus;| BitOrAfterMultiply | BitOrGrouped | PlusThenAnd | AndThenPlus |
|---|---|---|---|
| 7 | 9 | 0 | 5 |

The logic levels cause more damage in real queries. AND binds tighter than OR. In the first query below, the condition keeps every x row, plus the y rows where n is above 2. The demo uses a table variable with four rows. Nothing is left behind.
DECLARE @Item TABLE (n int, s varchar(5)); INSERT @Item VALUES (1,'x'), (2,'y'), (3,'x'), (4,'y'); SELECT n, s FROM @Item WHERE s = 'x' OR s = 'y' AND n > 2 ORDER BY n; SELECT n, s FROM @Item WHERE (s = 'x' OR s = 'y') AND n > 2 ORDER BY n;
| Query | Rows returned (n) |
|---|---|
| Without parentheses | 1, 3 and 4 |
| With parentheses | 3 and 4 |
Three Traps With Numbers
Integer division. When both sides are integers, the result is an integer. 7/2*2 returns 6, because 7/2 becomes 3 first. 7*2/2 returns 7. Using a decimal such as 7/2.0*2 returns 7.000000. Likewise 10/4 returns 2, while 10/4.0 returns 2.500000. The order you write the operators in changes the answer. Adding a decimal anywhere changes the result type as well. Here 6/2*(1+2.0) returns 9.0, a numeric value, instead of the integer 9.
The negative sign. A unary minus is applied after a multiplication that follows it. So 100/-2*3 doesn’t mean (100/-2)*3. It means 100/-(2*3), and the result is -16, not -150. So 100/-2*3 has a different shape from 100/2*3: the minus sign pulls the 2*3 together first. Write 100/(-2)*3 and you get -150.
The caret. In T-SQL, ^ is a bitwise exclusive OR, not a power. 2^3 returns 1. Use POWER(2,3) for exponents. The sign matters here too: POWER(-2,2) returns 4, while -POWER(2,2) returns -4.
Do You Always Need Parentheses?
You could argue that parentheses everywhere make a query noisy. For one level of operators, that’s true, and 2+3+4 needs none. I add them whenever two levels meet, and always around a negative number after a division. Then nobody has to remember a rule.
What to Remember
The order of operations in T-SQL has no surprises once you know the four levels. Work from the inside out. Multiplication and division share a level, addition and subtraction share a level, and ties go left to right. T-SQL never guesses at a number next to a parenthesis, so write every operator.
When a result looks wrong, add parentheses to match the reading you expect and compare the two answers. Then keep the parenthesized version in the query. Each demo above uses only constants and a table variable, so there is nothing to clean up.
A formula is not a debate, it is a decision you write down with parentheses.
Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.
Discover more from SQL Authority with Pinal Dave
Subscribe to get the latest posts sent to your email.





37 Comments. Leave new
But why did you take the three out of the parentheses though ?
ANS IS 1
My nephew asked me this very question, saying that he had said “1”. I said that he was correct. Then he said that a friend of his had said “9”. I wondered how on earth anyone could get 9 from that equation. Looking it over again, I exclaimed, holy sh, it IS 9. I added that it should have been written differently to avoid such errors. Just a pair of parentheses would have done the trick: (6÷2)(2+1). I do acknowledge that the original formulation gives 9, not 1.
what about treating the equation as a fraction where the numerator is 6 and the denominator is 2(1+2)? Woudn’t that result in 1?
It depends on whether you use PEMDAS or PEJMDAS. The J in the acronym stand for JUXTIPOSITION. eg. 2(1+2). Juxtaposition happens before multiplication or division. Without the juxtaposition the expression would be 2*(1+2). If you use PEMDAS, the expression 6÷2(1+2) is equal to 9. If you use PEJMDAS, the expression 6÷2(1+2) is equal to 1.
Curiously, I asked ChatGPT which was correct, PEMDAS or PEJMDAS. It’s answer was PEMDAS. Then I asked it to solve the equation 6÷2(1+2). The answer given was 1.
It used PEJMDAS to solve the problem after saying it was not the correct method to solve the problem!
I’ve run into a problem with SQL Server, on this very topic, that I just cannot untangle.
I am converting/assembling separate geo coordinate parts into a single decimal degree value.
It is straightforward (IMHO), and I’ve done it dozens of times, successfully, in various contexts (Excel formulas or SQL Queries etc.)
Recently, I tried this in SQL Server, and it simply returns the wrong answer.
Seems to me it should be calculated as follows:
(Here – we’ll just use latitude for example, and let “Dir” refer to the hemisphere (N or S), and using N as positive latitude:
Lat_Dec_Deg = iif(Lat_Dir = ‘S’, -1, 1) * (Lat_Degrees + Lat_Minutes/60 + Lat_Seconds/3600)
(You’ll have to give me a little latitude (no pun intended) on the use of “iif” — you get the gist. In SQL Server it seems one must instead use the ‘case’ command:
(case lat_direction when ‘S’ then -1 else 1 end) * (lat_degrees + lat_minutes/60 + lat_seconds/3600)
I don’t see any room for false interpretation in this. The division operations MUST happen before the additions, and I’m thinking that anything to do with this ‘unary’ negative 1 thing should be handled with the parentheses.
As far as I can tell, SQL Server does NOT reliably generate the right answer with this formula. And I can’t even figure out what it IS doing. (Example: 39 deg, 56 min, 29.3 sec North: In SQL Server it gives 39.008138. This is clearly wrong. 56 minutes puts it darn near 40 degrees. According to my calculations it should be about 39.9415.)
Am I missing the forest for the trees here? Or does anyone else agree that the above should work as written?
The purpose of the calculations is important. If this calculation is to solve a problem related to applied physics like space travel, then it is an algebraic equation and therefore juxtaposition has to be factored. You can not Willy Nilly add additional mathematical symbols to justify your answer. The lack of a symbol is relevant and indicates an expression like ab and must be handled as if it is in the parentheses itself.