Skip to content
Advertisement

SQL: Give the names of employees who earn more than their manager

Hi guys, I have this type of table, and I need to show the employees who earn more than there managers? It would be easier if we had 2 tables, but I cannot imagine a code within one table. Any ideas?

Advertisement

Answer

  • You can do “self-join” between the two tables. In the second table, EMPNO will be equal to the corresponding MGR value.
  • Use Where to filter out cases where salary of employee is higher than that of manager.

Try:


As @Strawberry suggested in comments that column comparisons can be done in the join condition itself (instead of using Where). You can do the following as well:

The advantage of this approach is that we have to switch from Inner Join to Left Join, changes required to the query would be lesser.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement