Groups | Blog | Home
all groups > sql server mseq > january 2004 >

sql server mseq : Precedehce rules for AND and OR in WHERE clauses


Ravi2
1/21/2004 9:26:08 PM
Hi folks,
I've been having a debate with colleagues about precedence rules over AND and OR, regarding how compound conditions are evaluated where no parentheses (brackets) have been used. The real question is "Are there industry standards over the precedence rules for AND and OR?"

Take, for example, the following query - parentheses deliberately omitted:
(1)
"SELECT * FROM x
WHERE
condition_a AND condition_b OR condition_c AND condition_d AND condition_e OR condition_f"

This could be interpreted in a number of ways, a couple of them being as follows:
(2)
"SELECT * FROM x
WHERE
( condition_a AND condition_b ) OR ( condition_c AND condition_d AND condition_e ) OR condition_f"

(3)
"SELECT * FROM x
WHERE
((((( condition_a AND condition_b ) OR condition_c ) AND condition_d) AND condition_e) OR condition_f)"

etc. etc.

My take is that:
1) each DB vendor is entitled to its own interpretation, and so the results are not portable across different vendors
2) even for a given vendor (e.g. MS-SQL 2000) the interpretation results may not GENERALLY predictable (although a SPECIFIC query may be evaluated in a specific way).

Does anyone have quotable "book knowledge" on this subject (either from a standards body, e.g. ANSI-SQL-92, or from a vendor, e.g. MS SQL 2000)?

Regards
Dandy Weyn
1/24/2004 10:54:59 AM
Have a look at 'Inside SQL Server' that explains well how these items are
processed in Microsoft SQL Server
--
Regards,

Dandy Weyn
MCSE, MCSA, MCDBA, MCT

www.dandyman.net
[quoted text, click to view]
and OR, regarding how compound conditions are evaluated where no parentheses
(brackets) have been used. The real question is "Are there industry
standards over the precedence rules for AND and OR?"
[quoted text, click to view]
may not GENERALLY predictable (although a SPECIFIC query may be evaluated in
a specific way).
[quoted text, click to view]

AddThis Social Bookmark Button