Home › Interview Questions › Given a dataset of employees and managers, write a…

Given a dataset of employees and managers, write a query to fetch all employees who earn more than their managers.

🟡 Medium Conceptual Junior level
1Times asked
Oct 2026Last seen
Oct 2026First seen

💡 Model Answer

Assuming a table employees with columns emp_id, manager_id, and salary, you can use a self‑join or a correlated subquery. Here’s a concise solution with a self‑join: ``sql SELECT e.emp_id, e.name, e.salary, m.name AS manager_name, m.salary AS manager_salary FROM employees e JOIN employees m ON e.manager_id = m.emp_id WHERE e.salary > m.salary; `` This returns each employee whose salary exceeds that of their direct manager. If you need to consider indirect managers, replace the join with a recursive CTE that walks up the hierarchy before comparing salaries.

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 on a discreet on-screen overlay.

Get Assisting AI — Starts at ₹500