6

Can anyone tell me what the result to the following should be according to the standard (a reference to the correct part of the standard would be welcome)

> select * from t1;
+------+
| col1 |
+------+
|    9 |
|    8 |
|   10 |
+------+
> update t1
    set col1 = col1 * 2
    where col1 <= (select avg(col1) from t1);

The point is: Does the last row get updated, since if the rows are updated in order and the average is recalculated for each row, it will satisfy the condition, or does it not get updated because any data changed by this statement will only be readable after the whole statement ran?

EDIT And what about this case?

> select * from t1;
+------+------+
| col1 | col2 |
+------+------+
|    9 |    1 |
|    8 |    2 |
|   10 |    2 |
+------+------+
> update t1 p1
    set col1 = col1 * 2
    where col1 <= (select avg(col1)
                     from t1
                     where col2=p1.col2);
3
  • Here the subquery executed first. So average doesn't not change. Apr 1, 2012 at 8:42
  • @Shiplu Thank you. And what about this second case, where the subquery can't be pre-executed?
    – Baruch
    Apr 1, 2012 at 8:49
  • 1
    @baruch - It can be pre-executed in that a separate read operation identifies the rows to be updated and stores the row identifiers in a spool or similar then the update is done reading from that spool after the read is finished. That is the plan I would expect in SQL Server. Not sure I can be bothered digging through the SQL Standard to find the relevant bits though. A couple of related concepts are "all–at–once operations" and halloween protection. Apr 1, 2012 at 8:55

3 Answers 3

4

As far as I can tell, the standard (Chapter 14.11, SQL 2003 - Foundation) is pretty clear about this:

The is effectively evaluated for each row of T before any row of T is updated

(emphasis mine)

My understanding of that sentence is that any condition (whether co-related or not) is evaluated before any row is updated.

4
  • ... and one popular (if not the only DBMS) that does NOT conform with the standard - and similar to the examples queries may result in non-standard results - is MySQL. Apr 1, 2012 at 9:55
  • @ypercube: why am I not surprised?
    – user330315
    Apr 1, 2012 at 9:56
  • 1
    To be honest, this is one of the most serious, if not the most serious, deviation from the standards. Even simple UPDATE t SET id = id + 1 are done in serial manner and not as a set operation, and will result in PK violations. Apr 1, 2012 at 9:58
  • @ypercube: there is also the case of deleting rows from a self referencing table: sqlfiddle.com/#!2/b05b1/1 works fine in Oracle, PostgreSQL and others, but not in MySQL. btw: the id = id + 1 only works in Postgres sincd 9.0 and deferrable unique constraint
    – user330315
    Apr 1, 2012 at 10:03
3

About the first query, the subquery executed first so, there is no changes in average...

About second query, you are using alias in UPDATE statement but you are using alias in wrong approach.

Correct and Standard way to use alias in UPDATE statement is:

UPDATE p1
     set col1 = col1 * 2
from t1 p1
     where col1 <= (select avg(col1)
                     from t1
                     where col2=p1.col2);
3
  • 2
    The SQL standard does not allow a FROM clause for an UPDATE statement. So your example is not "the standard way". Additionally in the SQL standard the table alias is defined in the UPDATE part: UPDATE <target table> [ [ AS ] <correlation name> ]. The above syntax is only valid for MySQL if I'm not mistaken
    – user330315
    Apr 1, 2012 at 9:17
  • @ a_horse_with_no_name:Thank you....It works on MS-SQL Server..I have already tested above query in MSSQL But when I execute OP's second query, it throws error...
    – Akash KC
    Apr 1, 2012 at 9:22
  • Check the behaviour of SQL-Server in Data Exchange: SQL standard and dependent conditionals in UPDATE Apr 1, 2012 at 9:51
2

The last row would not get updated. because "select avg(col1) from t1" is a sub-query, and it will run firstly, and store the result in the temporary table, then the update statement will execute.

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.