database - SQL UPDATE Query Using FROM -


while answering test faced following question, wasn't able solve:

given following table z , query:

table z: | value | --------- |   1   | |   2   | |   3   | |   4   | --------- 

query:

update z      set value = y.value + 1     z y      y.value = z.value + 1;  select sum(value) z; 

the question asks result of query execution. question doesn't mention specific sql language.

the correct answer 16.

i don't know how query can achieve result. wasn't able execute query in real environment, complains syntax error near "from".

1 - guys know how query works? 2 - how proceed in order execute query?

p.s. had hard time trying find information clause inside update query.

one database code work postgres. according rextester, indeed answer.

the reason should because adding "2" each matching z value: z = y.value + 1 = z.value + 1 + 1 -- fourth value not match. postgres generates following:

    value 1   4 2   3 3   4 4   5 

this same data in different order.

with similar statement, sql server right thing:

update z      set val = y.val + 1     z, z y      y.val = z.val + 1; 

(i using dreaded comma in from clause keep 2 statements similar possible.)

it returns:

    val 1   3 2   4 3   5 4   4 

the 2 result sets same, in different order.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -