CREATE TABLE `C` ( `int_nokey` int(11) DEFAULT NULL, `int_key` int(11) DEFAULT NULL, `varchar_nokey` varchar(1) DEFAULT NULL, KEY `int_key` (`int_key`), KEY `varchar_key` (`int_key`) ) DEFAULT CHARSET=latin1; INSERT INTO `C` VALUES (9,3,'m'); CREATE TABLE `D` ( `int_nokey` int(11) DEFAULT NULL, `int_key` int(11) DEFAULT NULL, `varchar_nokey` varchar(1) DEFAULT NULL, KEY `int_key` (`int_key`), KEY `varchar_key` (`int_key`) ) DEFAULT CHARSET=latin1; INSERT INTO `D` VALUES (1,5,'j'),(8,NULL,'f'); EXPLAIN EXTENDED SELECT table2 .`int_key` field12 , ( SELECT SUM( `int_nokey` ) FROM D WHERE `int_key` = table1 .`int_key` ) FROM C table1 RIGHT JOIN C table2 ON table1 .`varchar_nokey` ; SHOW WARNINGS; SELECT table2 .`int_key` field12 , ( SELECT SUM( `int_nokey` ) FROM D WHERE `int_key` = table1 .`int_key` ) FROM C table1 RIGHT JOIN C table2 ON table1 .`varchar_nokey` ; EXPLAIN EXTENDED SELECT table2 .`int_key` field12 , ( SELECT SUM( `int_nokey` ) FROM D WHERE `int_key` = table1 .`int_key` ) FROM C table1 RIGHT JOIN C table2 ON table1 .`varchar_nokey` GROUP BY field12 ; SELECT table2 .`int_key` field12 , ( SELECT SUM( `int_nokey` ) FROM D WHERE `int_key` = table1 .`int_key` ) FROM C table1 RIGHT JOIN C table2 ON table1 .`varchar_nokey` GROUP BY field12 ; # Analysis of the expected result. First the right join: # table1.varchar_nokey is 'm' which evaluates to false # so right join outputs table2//table1 record like this: # 9 3 m null null null # thus table.int_key is null in the subquery, and so the WHERE # is false (as D.int_key = NULL is a false condition) # so the SUM is over an empty table so NULL per the manual; # so we get as output 3,NULL; # we have a single row so the GROUP BY should do nothing, # so we get as final output 3,NULL. # But 3,8 in mysql-next-bugfixing if there is GROUP BY.