From: Date: March 21 2007 10:54pm Subject: intended behaviour for subquery aggregate with outer column refs? List-Archive: http://lists.mysql.com/internals/34435 Message-Id: <4601A981.3060303@hybyte.com> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit I am trying to establish the wanted behaviour for certain queries. I had been looking at them according to the draft of the SQL:2003 that I have (which might not be correct in all aspects, as it only is a (late) draft). The mysql documentation does not specify anything on the behaviour, but I have now found some docs within the source code. Those docs in the source code, differ form my understanding of the draft. I wish to establish: a) is the draft I have correct on the subject b) is my understanding correct, and have I not missed out essential parts c) if yes to the above: Is there a reason for the difference in the current implementation, including the in-code documentation. Is there an intention to change this, and in which version(s)? looking at the 2 queries: select t1.a, (select count(t1.b) from t2 limit 1) # aggregated in t1 from t1 group by t1.a VERSUS select t1.a, from t1 where (select count(t1.b) from t2 limit 1) # aggregated in t2 group by t1.a Looking at the bit of the code-embedded docs, which I have marked as "important bit": In the first query "count(b)" can be aggregated in the outer query, as it appears in the outer query's "select list". Therefore mysql chooses the outer query as aggregation query for "count(b)" In the 2nd part, an attempt to to this, would lead to an error, as "count(b)" would then appear in the outer queries "where" clause, which is not permitted. MySql therefore chooses to do, what it used to do in version 4.1: The aggregation query for "count(b)" is the inner query. "b" is imported as a column from the outer query. This does go in-line with the documentation found inside the source code, as quoted below. However it does not seem to be the same as the SQL2003 draft. refering to parts 6a/b and 7 > 6) The aggregation query of a SFS is > determined as follows. Case: > a) If SFS has no aggregated column reference, then the aggregation > query of SFS is the innermost that contains SFS. > b) Otherwise, the innermost qualifying query of the aggregated column > references of SFS is the aggregation query of SFS. > 7) SFS shall be contained in the , , or > , > or of a or of its aggregation query.