Learn-SQL icon indicating copy to clipboard operation
Learn-SQL copied to clipboard

5. Get all Bands that have No Albums - sql_mode=only_full_group_by

Open threehappypenguins opened this issue 1 year ago • 1 comments

Since Workbench seems to have been done away with, I had installed the 8.4 server and the 8.0.38 workbench. Apparently with the 8.4 server, sql_mode=only_full_group_by is enabled by default. The problem with this is that

SELECT bands.name AS 'Band Name'
FROM bands
LEFT JOIN albums ON bands.id = albums.band_id
GROUP BY albums.band_id
HAVING COUNT(albums.id) = 0;

Does not work:

Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column
'record_company.bands.name' which is not functionally dependent on columns in GROUP BY clause;
this is incompatible with sql_mode=only_full_group_by

bands.name is a non-aggregated column in the SELECT statement. The solution, I think, is changing GROUP BY albums.band_id to GROUP BY bands.id, bands.name.

threehappypenguins avatar Aug 03 '24 16:08 threehappypenguins

Thank you! This works for me

SOMWHY avatar Feb 18 '25 07:02 SOMWHY