Employees Earning More Than Their Managers — Day 92(SQL)

Photo by Mufid Majnun on Unsplash

Today’s question is a SQL question. Let us look into the problem statement.

181. Employees Earning More Than Their Managers

The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.

+----+-------+--------+-----------+
| Id | Name | Salary | ManagerId |
+----+-------+--------+-----------+
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | NULL |
| 4 | Max | 90000 | NULL |
+----+-------+--------+-----------+

Given the Employee table, write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.

+----------+
| Employee |
+----------+
| Joe |
+----------+

We have a table that contains some information about the employees working in an organization. Each employee has an Id, Name, Salary, and ManagerId. If we observe the values in the ManagerId column is from the Id column in the table. We need to find those employees who earn more than their manager.

How would we know about the salaries of the managers? Well, we know the salary of each employee, we just need to self join the Employee table to get to know the salaries of managers. From there, we can check if the employees earn more than their managers.

Let us look into the code snippet.

SELECT A.Name AS Employee
FROM Employee A, Employee B
WHERE A.Salary > B.Salary
AND A.ManagerId = B.Id;

Software Engineer. Find me @ www.linkedin.com/in/annamariya-jt

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store