# Logical Operators

• paul.knibbs (5/24/2012)

This is something I pretty much never bother to remember, because if there's any ambiguity I always add parentheses...makes it easier for humans to understand that way.

I get you, but for e.g. "OR has precedence over NOT" in T-SQL is not a fact. That was my comment about the QotD.

Igor Micev,My blog: www.igormicev.com

• IgorMi (5/24/2012)

I get you, but for e.g. "OR has precedence over NOT" in T-SQL is not a fact. That was my comment about the QotD.

Sorry...my post wasn't intended to be a direct answer to yours, just a general observation!

I can't believe that 21% have currently got it completely wrong, thinking that OR has precedence over AND! Hopefully they're just all misunderstanding what 'precedence' means π

I think that's what it is... I got it wrong.

The description makes sense... but I was looking at it as

A AND B OR C

In order to return from the select either A AND B must be true, OR C must be true.

since C isn't constrained by A AND B, the OR has precedence. π

Of course I misread the whole question and was thinking preference... heh...

I'm sure I thought of it wrong... obviously... but knowing how they work together and remembering what the definition of precedence is are really two different things.

--Mark Tassin
MCITP - SQL Server DBA
Proud member of the Anti-RBAR alliance.
For help with Performance click this link[/url]
For tips on how to post your problems[/url]

DugyC (5/24/2012)

Basic computer science question, nice and easy, thanks.

Can't believe almost a third got it wrong!

Bak to skool! π

"computer science" question? Since when was T-SQL operator precedence part of computer science? Do you think that all computer languages give AND precedence over OR? If so you will be surprised when you come across languages that give OR precedence over AND, languages that have left to right precedence, languages that have right to left precedence, and languages where not fixing evaluation order by overt syntactic means (using brackets or dots) is a syntax error.

LOL!! That's brilliant Tom, yeesh! :rolleyes:

Since when was T-SQL operator precedence part of computer science?

It's not but if you remove "T-SQL" it is.

Do you think that all computer languages give AND precedence over OR?

No I don't

_____________________________________________________________________
[font="Comic Sans MS"]"The difficult tasks we do immediately, the impossible takes a little longer"[/font]

• I'm with a couple of the guys here and I actually had to dig down into the think tank a little to remember it. I'd never write anything (or at least attempt to) without parenthesis to begin with, so it was actually a pretty nice challenge to remember it especially with it being so early. But, thanks to the QotD, my brain juices are flowing and I can start my day. π

Everything is awesome!

DugyC (5/24/2012)

Basic computer science question, nice and easy, thanks.

Can't believe almost a third got it wrong!

Bak to skool! π

"computer science" question? Since when was T-SQL operator precedence part of computer science? Do you think that all computer languages give AND precedence over OR? If so you will be surprised when you come across languages that give OR precedence over AND, languages that have left to right precedence, languages that have right to left precedence, and languages where not fixing evaluation order by overt syntactic means (using brackets or dots) is a syntax error.

I had exactly the same reaction to this idea that there is some sacred order in CS for AND and OR. I think this might be a question of assuming that the language you know best represents the way things "ought" to be. I've written code in 20+ languages (probably way less than Tom), and have just come to assume that they all have shortcomings! π

[font="Verdana"]Please don't go. The drones need you. They look up to you.[/font]

• Nice question! Order of operations π

I came up with question after seeing some code written by a colleague who had omitted to use brackets in the WHERE clause.

:exclamation: "Be brave. Take risks. Nothing can substitute experience." :exclamation:

• mtassin (5/24/2012)

I can't believe that 21% have currently got it completely wrong, thinking that OR has precedence over AND! Hopefully they're just all misunderstanding what 'precedence' means π

I think that's what it is... I got it wrong.

The description makes sense... but I was looking at it as

A AND B OR C

In order to return from the select either A AND B must be true, OR C must be true.

since C isn't constrained by A AND B, the OR has precedence. π

Of course I misread the whole question and was thinking preference... heh...

I'm sure I thought of it wrong... obviously... but knowing how they work together and remembering what the definition of precedence is are really two different things.

That's exactly how I was thinking about it too.

• An easy one - thanks!

• Nice easy one, thanks!

• nice, straight forward questions today - cheers

• JiCiT (5/24/2012)

mtassin (5/24/2012)

I can't believe that 21% have currently got it completely wrong, thinking that OR has precedence over AND! Hopefully they're just all misunderstanding what 'precedence' means π

I think that's what it is... I got it wrong.

The description makes sense... but I was looking at it as

A AND B OR C

In order to return from the select either A AND B must be true, OR C must be true.

since C isn't constrained by A AND B, the OR has precedence. π

Of course I misread the whole question and was thinking preference... heh...

I'm sure I thought of it wrong... obviously... but knowing how they work together and remembering what the definition of precedence is are really two different things.

That's exactly how I was thinking about it too.

Add me to this list too. I answered OR having higher precedent than AND because it doesn't matter what the evaluation of one side of the OR is if the other side evaluates true therefore the OR is of a higher "precedent", but I didn't take into account how Microsoft defines "precedent". Oh well.

• Good question. I learned something today. Thanks for submitting.

http://brittcluff.blogspot.com/

• mtassin (5/24/2012)I think that's what it is... I got it wrong.

The description makes sense... but I was looking at it as

A AND B OR C

In order to return from the select either A AND B must be true, OR C must be true.

since C isn't constrained by A AND B, the OR has precedence. π

Of course I misread the whole question and was thinking preference... heh...

I'm sure I thought of it wrong... obviously... but knowing how they work together and remembering what the definition of precedence is are really two different things.

Ahh, someone else got it wrong.

I was afraid of posting anything because almost everyone stated this was an easy question. But I was thinking along the same line: the OR operator will "overcome" all of the AND operators in the end. :hehe:

Best regards,

Andre Guerreiro Neto

Database Analyst
http://www.softplan.com.br
MCITPx1/MCTSx2/MCSE/MCSA

Viewing 15 posts - 16 through 30 (of 40 total)