SQLTeam.com | Weblogs | Forums

What is the sequence of execution of AND statements?

Hello

If a statement has the following criteria:

WHERE [1] NOT IN ('a','b','c')
AND CASE WHEN [1] IS 'a' THEN [2] ELSE [1] END <> 'b'

How will SQL process this? Will it do the first filtering and then the CASE?

Thanks!

SQL can do independent clauses like that in whatever order it chooses to do them.

If you want to enforce an order, you can embed all of it in a CASE statement; CASE statements are always evaluated in order.

For example, if you write:

WHERE <condition_1> AND <condition_2> AND <condition_3>

then SQL can evaluate either cond_1 or _2 or _3 first, and any of the others next, as SQL decides is best.