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;
Hi Jose!
Bravo! This is also a correct solution!
Hope this helps.
Best,
Tsvetelin
haha yes, I was also thinking the same why are they not using GROUP BY
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
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.