Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execution order of WHEN clauses in a CASE statement

Given the following body of a case statement:

1    WHEN r.code= '00'                        then 'A1' 2    WHEN r.code ='01' AND r.source = 'PXWeb' then 'A2'   < 3    WHEN r.code ='0120'                      then 'A3' 4    WHEN r.code ='01'                        then 'A4'   < 5    WHEN r.code ='1560'                      then 'A5' 6    WHEN r.code ='1530'                      then 'A6' 7    WHEN r.code ='1550'                      then 'A7' 

I'm assuming line 2 will always execute before line 4? Then I read statements like 'SQL is a declarative language, meaning that it tells the SQL engine what to do, not how' in

Order Of Execution of the SQL query

and wonder if this also relates to the order of execution in the CASE statement. Essentially, can i leave the code above as it is without having to change line 4 to

4    WHEN r.code ='01' AND r.source != 'PXWeb' then 'A4'    
like image 743
The Ghost Avatar asked Jul 02 '14 10:07

The Ghost


People also ask

What is the order of execution of clauses?

The SQL order of execution defines the order in which the clauses of a query are evaluated. Some of the most common query challenges people run into could be easily avoided with a clearer understanding of the SQL order of execution, sometimes called the SQL order of operations.

How does a case statement execute?

The CASE statement selects a sequence of statements to execute. To select the sequence, the CASE statement uses a selector (an expression whose value is used to select one of several alternatives) or, in the searched CASE statement, multiple search conditions.

How do you use CASE statement in order?

CASE Syntax: CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 WHEN condition3 THEN result3 ELSE result END; ORDER BY: This keyword is used to sort the result-set in ascending or descending order. It sorts the records in ascending order by default.

Does the order of CASE statements matter?

Yes, the order of the case statements does matter. The first matching row will be the one returned.


1 Answers

The value that is returned will be the value of the THEN expression for the earliest WHEN clause (textually) that matches. That does mean that if your line 2 conditions are met, the result will be A2.

But, if your THEN expressions were more complex than just literal values, some of the work to evaluate those expressions may happen even when that expression is not required.

E.g.

 WHEN r.code= '00'                        then 'A1'  WHEN r.code ='01' AND r.source = 'PXWeb' then 'A2'  WHEN r.code ='0120'                      then 1/0  WHEN r.code ='01'                        then 'A4' 

could generate a division by zero error even if r.code isn't equal to 0120, and even if it's equal to 00, say. I don't know what the standard has to say on this particular issue but I know that it is true of some products.

like image 168
Damien_The_Unbeliever Avatar answered Sep 22 '22 11:09

Damien_The_Unbeliever