MySQL Handling of GROUP BY mysql>  CREATE TABLE mytable ( -> id INT UNSIGNED NOT NULL PRIMARY KEY, -> a VARCHAR(10), -> b VARCHAR(10), -> c INT ->  ); mysql>  INSERT INTO mytable ->  VALUES (1, ’abc’, ’qrs’, 1000), -> (2, ’abc’, ’tuv’, 2000), -> (3, ’def’, ’qrs’, 4000), -> (4, ’def’, ’tuv’, 8000), -> (5, ’abc’, ’qrs’, 16000), -> (6, ’def’, ’tuv’, 32000); mysql>  SELECT @@session.sql_mode; +---------------------------------------------------------------+ | @@session.sql_mode | +---------------------------------------------------------------+ | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION | +---------------------------------------------------------------+ mysql>  SELECT a, b, SUM(c) FROM mytable -> WHERE a = ’abc’ AND b = ’qrs’; +------+------+--------+ | a | b | SUM(c) | +------+------+--------+ | abc | qrs | 17000 | +------+------+--------+ If  ONLY_FULL_GROUP_BY  is disabled, a MySQL extension to the standard SQL use of  GROUP BY  permits the select list,  HAVING  condition, or  ORDER BY  list to refer to nonaggregated columns even if the columns are not functionally dependent on  GROUP BY  columns. This causes MySQL to accept the preceding query. In this case, the server is free to choose any value from each group, so unless they are the same, the values chosen are nondeterministic, which is probably not what you want. Furthermore, the selection of values from each group cannot be influenced by adding an  ORDER BY  clause. Result set sorting occurs after values have been chosen, and  ORDER BY  does not affect which value within each group the server chooses. Disabling  ONLY_FULL_GROUP_BY  is useful primarily when you know that, due to some property of the data, all values in each nonaggregated column not named in the  GROUP BY  are the same for each group. You can achieve the same effect without disabling  ONLY_FULL_GROUP_BY  by using  ANY_VALUE()  to refer to the nonaggregated column. The following discussion demonstrates functional dependence, the error message MySQL produces when functional dependence is absent, and ways of causing MySQL to accept a query in the absence of functional dependence. This query might be invalid with  ONLY_FULL_GROUP_BY  enabled because the nonaggregated  address column in the select list is not named in the  GROUP BY  clause: The query is valid if  name  is a primary key of  t  or is a unique  NOT NULL  column. In such cases, MySQL recognizes that the selected column is functionally dependent on a grouping column. For example, if  name is a primary key, its value determines the value of  address  because each group has only one value of the primary key and thus only one row. As a result, there is no randomness in the choice of  address  value in a group and no need to reject the query. The query is invalid if  name  is not a primary key of  t  or a unique  NOT NULL  column. In this case, no functional dependency can be inferred and an error occurs: SELECT name, address, MAX(age) FROM t GROUP BY name; 2412