SQL SERVER – Order of Conditions in WHERE Clause

Conditions in a WHERE clause don’t define a short-circuit evaluation sequence. I inspect actual behavior instead of relying on text order.

The same two sieve frames appear in reversed positions in adjacent workshop channels.

A senior developer asked whether moving predicates changes speed or short-circuit behavior. My earlier answer was too absolute. SQL text order doesn’t promise evaluation order. Equivalent expressions also don’t guarantee identical plans in every case.

The original challenge

  • Use any number of tables.
  • Change only the order of the conditions, preserving parentheses and Boolean meaning.
  • Use AND or OR between the conditions.
  • Compare actual execution plans and SET STATISTICS IO output.
  • Return the same results before and after the change.

Use repeatable test conditions and record the server build, compatibility level, data, parameters, and cache state. An incidental timing difference is not enough to establish a query improvement.

Don’t rely on a preceding predicate to protect a later unsafe conversion or division. Express the required handling deliberately, then test its behavior.

The challenge remains open here, with the original example article and performance-testing resources below. Share a reproducible example and the evidence supporting it.

Related reading

Predicate order is not an evaluation contract, it is one way to express the required conditions.

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.

SQL Scripts, SQL Server
Previous Post
SQL SERVER – PIVOT and UNPIVOT Table Examples
Next Post
When to Upgrade: RTM, First CU or Later

Related Posts

5 Comments. Leave new

  • I disagree.

    case in point:

    If you have a column which contains both text and integers and you try to query only int. using ismumeric funtion, it matters where you place this check in your where clause.

    Following will fail:

    select
    from
    where convert(int, txtCol) = 1
    and isnumeric(txtCol) = 1

    Following will work:

    select
    from
    where isnumeric(txtCol) = 1
    and convert(int, txtCol) = 1

    At least this is my obersvation.

    Reply
  • I have been trying to research this, issue as well as the effect of the order of conditions in a join (separated by AND).

    For example, if Table b is partitioned on Col3, could we see a performance improvement by listing a.Col3 = b.Col3 first in the following join?

    SELECT a.Col1, b.col1
    FROM TableA AS a
    INNER JOIN TableB AS b
    ON a.Col1 = b.Col1
    AND a.col2 = b.Col2
    AND a.Col3 = b.Col3

    Some people say that the optimizer will do this internally. I asked this question of another SQL Server Expert and was told that they are processed in order, so it does make a difference. He provided the following queries as evidence:

    This query will not error. The first condition is met, so the 2nd condition is never processed.

    select 1
    where 1=0
    and cast(‘bob’ as datetime)=’hello’

    This query will error since the first condition is impossible.

    select 1
    where 1=1
    and cast(‘bob’ as datetime)=’hello’

    This was shown as proof that conditions are processed in order. He said that join conditions are processed in the same way.

    My initial thinking is that if the column on which the table is partitioned is listed first, SQL Server would only have consider that single partition, saving on reads of the others disks and therefore improving performance of that stored procedure and of the database overall.

    When I tested this however, I was unable to see any difference in the execution plan.

    If there is no difference, can you explain to me how SQL Server does this, and why the above queries operate as they do?

    Thank you

    Reply
  • Also SQL Server 2005 does support Short circuit conditions. I think it was not supported in SQL Server 2000.

    Reply
  • This is an interesting debate. I am a PeopleSoft developer and have worked extensivvely on SQLs. I think order of clause has very less impact.

    To justify what I say I have a simple example:
    There is a big query which joins many tables and so has various mappings. Now I want this query to run conditionally. So I declare a variable outside the SQL, and use it in the SQL. Where ever I place this fairly simple condition ( substitued condition looks like ‘C’ = ‘C’ or ‘C’ = ‘F’) the query exectution doesnt change much. It takes aprox 4 secs where a false boolean is encountered.

    When the amount of data to be fetched is less, on re-executing the same query many times we get different timings. This has various parameters like was the Query compiled/executed recnetly, etc.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.