November 30, 2007 at 9:09 am
Does SQL evaluate both parts of an OR condition or does it stop when one condition is TRUE? For example:
SELECT *
FROM Company
WHERE Company_Id = 'ABC'
OR Company_Name = 'ACME Corporation'
While rolling through the table will second part of the OR condition be evaluated when record Company_Id = 'ABC' is encountered? Or will this second be skipped because the first condition is TRUE?
November 30, 2007 at 11:20 am
>>Does SQL evaluate both parts of an OR condition or does it stop when one condition is TRUE? For example:
No guarantees what it will do. SQL uses a cost based optimizer, so how it evaluates an OR condition behind the scenes will depend on several factors:
- Availability of indexes containing the columns
- Data distribution of values in the columns
- Up to date statistics on the table/columns
November 30, 2007 at 12:39 pm
From BOL:
The order of precedence for the logical operators is NOT (highest), followed by AND, followed by OR. Parentheses can be used to override this precedence in a search condition. The order of evaluation of logical operators can vary depending on choices made by the query optimizer. For more information about how the logical operators operate on logic values, see AND (Transact-SQL), OR (Transact-SQL), and NOT (Transact-SQL).
Cheers,
* Noel
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply