SQL
All Top 50 SQL Interview Questions#13
Self-Joins for Organizational Hierarchy in SQL
MediumHondaInterview Question #13
Asked at HondaGiven an `employees` table (emp_id, emp_name, manager_id), write a query to list each employee along with their direct manager's name. If an employee has no manager (e.g. CEO), display 'Top Executive'.
Input Table: employees
4 rows preview| emp_id | emp_name | manager_id |
|---|---|---|
| 1 | Rajesh Mehta | NULL |
| 2 | Kaushal Kumar | 1 |
| 3 | Pooja Sharma | 1 |
| 4 | Ankit Verma | 2 |
Expected Output Structure4 rows
| emp_id | employee_name | manager_name |
|---|---|---|
| 1 | Rajesh Mehta | Top Executive |
| 2 | Kaushal Kumar | Rajesh Mehta |
| 3 | Pooja Sharma | Rajesh Mehta |
| 4 | Ankit Verma | Kaushal Kumar |
Interview Context
Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.
PostgreSQL 15
Ready to execute
Click "Run Query" or press Ctrl + Enter to test your query.
Output will be verified against the test dataset.