Given the EMPLOYEE table, write a query to retrieve each employee’s name along with their manager’s name.
💡 Model Answer
To get each employee’s name and their manager’s name, you can perform a self‑join on the EMPLOYEE table. The manager_id column references the empId of the manager. The query looks like this:
SELECT e.name AS employee_name,
m.name AS manager_nameFROM EMPLOYEE e
LEFT JOIN EMPLOYEE m
ON e.manager_id = m.empId;
The LEFT JOIN ensures that employees without a manager (e.g., the CEO) still appear in the result set with a NULL manager_name. If you prefer to exclude top‑level employees, use an INNER JOIN instead. The query runs in O(n) time, where n is the number of rows, because the database can use an index on empId to perform the join efficiently. This approach is straightforward, readable, and works for any depth of hierarchy without recursion.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.
Get Assisting AI — Starts at ₹500