0

so I had this one SQL problem asked to me on a coding challenge and I initially understood it wrong. Basically given an Employee table, with a primary key being EmployeeID theres also a column named say ManagerID. That ManagerID references a primary key in the same Employee table. Let's say EmployeeID 1's Manager is EmployeeID 3 who has a manager with EmployeeID 4.

How would a query look like to list all the Employees next to their managers? I'm a novice when it comes to SQL but is there a way to do this with say a for/while loop since it's possible for a low level employee to link all the way up the chain of command many times?

animusdx
  • 380
  • 3
  • 16
  • 3
    Why are you taking a coding challenge on a topic you don't know. Your question is a rather basic SQL statement. I would suggest that you invest some time in learning SQL. – Gordon Linoff Apr 26 '19 at 15:20
  • 2
    Show what you've tried. Also, put sample data and expected results. But agree with @GordonLinoff, this is a simple statement. – SS_DBA Apr 26 '19 at 15:22
  • You could always try Googling for solutions to this pretty common problem. For example, https://stackoverflow.com/questions/27631884/recursive-cte-find-all-employees-below-manager – Richard Hansell Apr 26 '19 at 15:28
  • Now that I think about it, it is a pretty simple query. I was overthinking it because if say the table has 5 rows with primary keys from 1-5, a join query would go through every row and list that given rows manager. I think what I was thinking about that made it a bit more complicated was say a query for the lowest level employees that had managers who have managers who also have managers. – animusdx Apr 26 '19 at 15:29

0 Answers0