r/mysql • u/Local-Hovercraft8516 • Nov 29 '24
question Inner Join Question
The Employee table has the following columns:
- ID - integer, primary key
- FirstName - variable-length string
- LastName - variable-length string
- ManagerID - integer
Write a SELECT statement to show a list of all employees' first names and their managers' first names. List only employees that have a manager. Order the results by Employee first name. Use aliases to give the result columns distinctly different names, like "Employee" and "Manager".
Hint: Join the Employee table to itself using INNER JOIN.
Select FirstName, ManagerID
From Employee As E
Inner Join Employee As M
ON E.FirstName = M.FirstName
ORDER BY FirstName;
ERROR 1052 (23000) at line 2: Column 'FirstName' in field list is ambiguous
4
u/jhkoenig Nov 29 '24
Aren't you putting off doing your homework until the last minute?
Anyway, change the beginning to "Select E.FirstName, M.FirstName"
1
u/r3pr0b8 Nov 29 '24
the
Firstnamecolumn exists in both tablesconsequently, when you want to reference one of them, like in both your SELECT and ORDER BY clauses, you have to qualify which one you want...
... exactly like you did in the ON clause
which brings me to my main point -- your ON clause will return only those employees who have the same name as anyone else in the table, including themselves
which i'm pretty sure is not what was asked
2
u/sujaldhamija Nov 30 '24
The SELECT clause is missing aliases names and the ON clause of inner join is wrong. Seems like some assignment lol
0
u/Wiikend Nov 30 '24
I'm going to be direct here. If you're sensitive to honest and to-the-point feedback, stop reading now.
These kinds of questions is literally the best thing you can use ChatGPT for. It's designed for this kind of helpful conversational journeys. Try there first next time, please. This is obviously homework, and we're not tutors. Make an effort for your own learning's sake.
0
u/Qualabel Nov 30 '24
I'd start with
SELECT e.firstname employee, m.firstname manager... FROM... LEFT JOIN...- an INNER JOIN will exclude employees with no manager