Last answered:

09 Jan 2024

Posted on:

17 Jan 2022

4

Alternative code using group by

This code works as well and it is much simpler. Are there any cons I am not aware?

SELECT
    e1.*
FROM
    emp_manager e1
        JOIN
    emp_manager e2 ON e1.emp_no = e2.manager_no
GROUP BY e1.emp_no;

4 answers ( 0 marked as helpful)
Instructor
Posted on:

04 Feb 2022

0

Hi Jose!

Bravo! This is also a correct solution!

Hope this helps.
Best,
Tsvetelin

Posted on:

28 Sept 2022

0

haha yes, I was also thinking the same why are they not using GROUP BY

Posted on:

02 Feb 2023

0

Hi
I have tried using group by but i got the error code below. do you have any idea why?

SELECT em1.*
FROM emp_manager em1
JOIN emp_manager em2 ON em1.emp_no = em2.manager_id
GROUP BY em1.emp_no;



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

Thanks

Posted on:

09 Jan 2024

0

Hello Ashraf Shagar, your group by clause is missing the aggregated column.
Where you have the select. It should be SELECT em1.emp_no.

Lastly, your em2.manager_id was this what you used when creating the table in the emp_manager table? If not, you would need to also change it the right alias.

Submit an answer