There are several ways we can go about resolving this issue. Below are six options for dealing with this issue.
Example of Error First, here’s an example of code that produces the error:
SELECT District, Name, SUM(Population) FROM City WHERE CountryCode = 'AUS' GROUP BY District; Result:
ERROR 1055 (42000): Expression #2 of the SELECT list is not in the GROUP BY clause and c
Here, #2 means that the second expression in our SELECT list is causing the problem. In our case, it’s the Name column. We included the Name column in our SELECT list but not the GROUP BY clause.
Solution 1 Another way to deal with this issue is to include the Name column in the GROUP BY clause:
SELECT District, Name, SUM(Population) FROM City WHERE CountryCode = 'AUS' GROUP BY District, Name;
Result:
+-----------------+---------------+-----------------+ | District | Name | SUM(Population) | +-----------------+---------------+-----------------+ | New South Wales | Sydney | 3276207 | | Victoria | Melbourne | 2865329 | | Queensland | Brisbane | 1291117 | | West Australia | Perth | 1096829 | | South Australia | Adelaide | 978100 | | Capital Region | Canberra | 322723 | | Queensland | Gold Coast | 311932 | | New South Wales | Newcastle | 270324 | | New South Wales | Central Coast | 227657 | | New South Wales | Wollongong | 219761 | | Tasmania | Hobart | 126118 | | Victoria | Geelong | 125382 | | Queensland | Townsville | 109914 | | Queensland | Cairns | 92273 |
Solution 2 Another way to deal with this error is with the ANY_VALUE() function.
SELECT District, ANY_VALUE(Name), SUM(Population) FROM City WHERE CountryCode = 'AUS' GROUP BY District; Result:
+-----------------+-----------------+-----------------+ | District | ANY_VALUE(Name) | SUM(Population) | +-----------------+-----------------+-----------------+ | New South Wales | Sydney | 3993949 | | Victoria | Melbourne | 2990711 | | Queensland | Brisbane | 1805236 | | West Australia | Perth | 1096829 | | South Australia | Adelaide | 978100 | | Capital Region | Canberra | 322723 | | Tasmania | Hobart | 126118 | +-----------------+-----------------+-----------------+ The ANY_VALUE() function picks an arbitrary value from the specified column. In this case, it picked an arbitrary value from the Name column, and we got Sydney in the first row, Melbourne in the second, and so on. As we can see from the previous example, that column contains more than just those values for their respective districts, but MySQL has simply chosen one value to present.
Solution 3 Another option is to use the GROUP_CONCAT() function:
SELECT District, GROUP_CONCAT(Name), SUM(Population) FROM City WHERE CountryCode = 'AUS' GROUP BY District;
Solution 4 We only get the error when our sql_mode contains ONLY_FULL_GROUP_BY.
We can check our sql_mode like this:
SELECT @@sql_mode; Result:
ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION As expected, ONLY_FULL_GROUP_BY is enabled, which is why I got the error.
We can disable ONLY_FULL_GROUP_BY from our session’s sql_mode like this:
SET @@sql_mode = SYS.LIST_DROP(@@sql_mode, 'ONLY_FULL_GROUP_BY'); SELECT @@sql_mode; Result:
STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION By using the SYS.LIST_DROP system function, we removed the item from the list without affecting any other items. We can see that ONLY_FULL_GROUP_BY is no longer in our sql_mode.
Let’s now run the original query that caused the error:
SELECT District, Name, SUM(Population) FROM City WHERE CountryCode = 'AUS' GROUP BY District; Result:
+-----------------+-----------+-----------------+ | District | Name | SUM(Population) | +-----------------+-----------+-----------------+ | New South Wales | Sydney | 3993949 | | Victoria | Melbourne | 2990711 | | Queensland | Brisbane | 1805236 | | West Australia | Perth | 1096829 | | South Australia | Adelaide | 978100 | | Capital Region | Canberra | 322723 | | Tasmania | Hobart | 126118 | +-----------------+-----------+-----------------+
group by.remove it.