10. Employees Earning More Than Their Manager

A company wants to identify employees whose salary is higher than their direct manager’s salary to review compensation structure.

Table: Employees

╔═════════════╦══════════╗
β•‘ Column Name β•‘   Type   β•‘
╠═════════════╬══════════╣
β•‘ employee_id β•‘   int    β•‘
║─────────────┼──────────║
β•‘    name     β•‘ varchar  β•‘
║─────────────┼──────────║
β•‘   salary    β•‘   int    β•‘
║─────────────┼──────────║
β•‘ manager_id  β•‘   int    β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•
  • employee_id is the primary key.
  • manager_id refers to another employee.
  • The top manager has manager_id = NULL.

Write an SQL query to return the names of employees whose salary is strictly greater than their manager’s salary. Return the result in any order.

Example 1:

Input:

Employees:

╔═════════════╦══════════╦══════════╦════════════╗
β•‘ employee_id β•‘   name   β•‘  salary  β•‘ manager_id β•‘
╠═════════════╬══════════╬══════════╬════════════╣
β•‘      1      β•‘  Ritesh  β•‘  90000   β•‘    NULL    β•‘
║─────────────┼──────────┼──────────┼────────────║
β•‘      2      β•‘  Ananya  β•‘  95000   β•‘     1      β•‘
║─────────────┼──────────┼──────────┼────────────║
β•‘      3      β•‘  Mohit   β•‘  85000   β•‘     1      β•‘
║─────────────┼──────────┼──────────┼────────────║
β•‘      4      β•‘  Kunal   β•‘  97000   β•‘     2      β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•©β•β•β•β•β•β•β•β•β•β•β•β•β•

Output:

╔══════════╗
β•‘   name   β•‘
╠══════════╣
β•‘  Ananya  β•‘
║──────────║
β•‘  Kunal   β•‘
β•šβ•β•β•β•β•β•β•β•β•β•β•

Explanation:

  • Ananya (95000) > Ritesh (90000), included
  • Mohit (85000) < Ritesh (90000), excluded
  • Kunal (97000) > Ananya (95000), included

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Hints

0
Β 
Test Case

Input:

Employees