简体   繁体   中英

Logical operator AND having higher order of precedence than IN

I've read that logical operator AND has higher order of precedence than logical operator IN, but that doesn't make sense since if that was true, then wouldn't in the following statement the AND condition got evaluated before the IN condition ( thus before IN operator would be able to check whether Released field equals to any of the values specified within parentheses ?

SELECT Song, Released, Rating
FROM Songs
WHERE
Released IN (1967, 1977, 1987)
AND
SongName = ’WTTJ’

thanx

EDIT:

Egrunin and ig0774 , I've checked it and unless I totally misunderstood your posts, it seems that

WHERE x > 0 AND x < 10 OR special_case = 1

is indeed the the same as

WHERE (x > 0 AND x < 10) OR special_case = 1

Namely, I did the the following three queries

SELECT * 
FROM Songs 
WHERE AvailableOnCD='N' AND Released > 2000 OR Released = 1989

SELECT *
FROM Songs
WHERE (AvailableOnCD='N' AND Released > 2000) OR Released = 1989

SELECT *
FROM Songs
WHERE AvailableOnCD='N' AND (Released > 2000 OR Released = 1989)

and as it turns out the following two queries produce the same result:

SELECT * 
FROM Songs 
WHERE AvailableOnCD='N' AND Released > 2000 OR Released = 1989

SELECT *
FROM Songs
WHERE (AvailableOnCD='N' AND Released > 2000) OR Released = 1989

while

SELECT *
FROM Songs
WHERE AvailableOnCD='N' AND (Released > 2000 OR Released = 1989)

gives a different result

I'm going to assume you're using SQL Server, as in SQL Server AND has a higher order of precedence than IN . So, yes, the AND is evaluated first, but the rule for evaluating AND , is to check the expression on the left (in your sample, the IN part) and, if that is true, the expression on the right. In short, the AND clause is evaluated first, but the IN clause is evaluated as part of the AND evaluation.

It may be simpler to understand the order of precedence here as referring to how the statement is parsed, rather than how it is executed (even if MS's documentation equivocates on this).


Edit in response to comment from the OP:

I'm not all together certain that IN being classified as a logical operator is not specific to SQL Server . I've never read the ISO standard, but I would note that the MySQL and Oracle docs define IN as a comparison operator, Postgres as a subquery expression, and Sybase itself as a "list operator". In my view, Sybase is the nearest to the mark here since the expression a IN (...) asks whether the value of attribute a is an element of the list of items between the parentheses.

That said, I might imagine the reason that SQL Server chose to classify IN as a logical operator is two-fold:

  1. IN and the like do not have the type restrictions of the SQL Server comparison operators ( = , != , etc. cannot apply to text, ntext or image types; IN and other subset operators can be used against any type, except, in strict ISO SQL, NULL )
  2. The result of an IN , etc. operation is a boolean value just like the other "logical operators"

Again, to my mind, this is not a sensible classification, but it is what Microsoft chose. Maybe someone else has further insight into why they may have so decided?

Call me a n00b, but I always use parentheses in nontrivial compound conditions.

SELECT Song, Released, Rating
FROM Songs
WHERE
    (Released IN (1967, 1977, 1987))
AND
    SongName = ’WTTJ’

Edited (Corrected, the point remains the same.)

Just yesterday I got caught by this. Started with working code:

WHERE x < 0 or x > 10

Changed it in haste:

WHERE x < 0 or x > 10 AND special_case = 1

Broke, because this is what I wanted:

WHERE (x < 0 or x > 10) AND special_case = 1

But this is what I got:

WHERE x < 0 or (x > 10 AND special_case = 1)

In Mysql at least, it has a lower precedence. See http://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html

I think of IN is a comparison operator whereas AND is a logical operator. So it's a bit apples and oranges, since the comparison operator must be evaluated first to see if the condition is true, then the logical operator is used to evaluate the conditions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM