Say Say
Say Say

Reputation: 13

SQL request foreign key on same table

I got a table T, with the following columns - ID_Employee (primary key) - NAME_Employee - ID_Manager (foreign key on the same table)

In order to display the rows of the table, I do : SELECT ID_Employee , NAME_Employee, ID_Manager FROM T

But how can I display the NAME of the manager instead of his ID? I'm a beginner on SQL :)

Thanks

Upvotes: 1

Views: 252

Answers (1)

Greg the Incredulous
Greg the Incredulous

Reputation: 1846

Try this:

SELECT a.ID_Employee, 
  a.NAME_Employee, 
  b.ID_Employee AS ManagerID, 
  b.Name_Employee AS Manager
FROM T a
INNER JOIN T b
ON a.ID_Manager = b.ID_Employee

Upvotes: 2

Related Questions