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

MySQL

Open jojohilvan24 opened this issue 4 years ago • 5 comments

jojohilvan24 avatar Jun 27 '20 15:06 jojohilvan24

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

In the last line instead of albums.id there should be albums.band_id

so the code should be

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

pranabroygithub avatar Jul 01 '21 15:07 pranabroygithub

Yes, you are right. But why should we use GROUP BY in this situation, wouldn't a simple WHERE return the same result. Something like this:

SELECT bands.name AS 'Band Name' FROM bands LEFT JOIN albums ON bands.id = albums.band_id WHERE albums.band_id IS NULL

@pranabroygithub What do you think?

lzyslh avatar Sep 27 '21 02:09 lzyslh

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

In the last line instead of albums.id there should be albums.band_id

so the code should be

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

I don't think there is any difference between them as 'id' and 'band_id' will both be NULL after a LEFT JOIN if a band does not have any albums.

lingyutan avatar Nov 12 '21 12:11 lingyutan

In Sql select bands.name as 'Band Name' from bands left join albums on bands.id = albums.band_id group by albums.id having count(albums.band_id) = 0; for me getting error Column 'bands.name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. anyone give solution plz

vinaykumar4312 avatar Sep 20 '22 07:09 vinaykumar4312

In Sql select bands.name as 'Band Name' from bands left join albums on bands.id = albums.band_id group by albums.id having count(albums.band_id) = 0; for me getting error Column 'bands.name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. anyone give solution plz

add the bands.name to the group by, like this:

select bands.name as 'Band Name' from bands left join albums on bands.id = albums.band_id group by albums.id, bands.name having count(albums.band_id) = 0;

Rodovs avatar Feb 21 '23 01:02 Rodovs