Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MySQL #1

Open
jojohilvan24 opened this issue Jun 27, 2020 · 5 comments
Open

MySQL #1

jojohilvan24 opened this issue Jun 27, 2020 · 5 comments

Comments

@jojohilvan24
Copy link

No description provided.

@pranabroygithub
Copy link

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;

@lzyslh
Copy link

lzyslh commented Sep 27, 2021

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?

@lingyutan
Copy link

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.

@vinaykumar4312
Copy link

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

@Rodovs
Copy link

Rodovs commented Feb 21, 2023

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;

iAMSieghart added a commit to iAMSieghart/Learn-SQL that referenced this issue Feb 25, 2023
Expression WebDevSimplified#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants