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.