Employees Earning More Than Their Managers SQL Challenge - Oracle Solution

1. Introduction

This blog post is dedicated to exploring a common query scenario in Oracle databases: finding employees who earn more than their managers. This problem is an excellent example of self-joins and conditional selection in SQL.

Problem

The goal is to write an Oracle SQL query that identifies employees whose salary exceeds that of their managers in a company's database.

Database Table:

Employee Table:
Id Name Salary ManagerId
101 John 6000 102
102 Jane 7500 NULL

3. Oracle Solution

SELECT e1.Name AS Employee
FROM Employee e1
JOIN Employee e2 ON e1.ManagerId = e2.Id
WHERE e1.Salary > e2.Salary;

Output:

Employee
John

Explanation:

1. The query uses a self-join to compare each employee (e1) with their respective manager (e2).

2. The join is performed on the ManagerId field.

3. The WHERE clause filters out those employees whose salary is greater than that of their manager.

4. The final output lists the names of employees earning more than their managers.


Comments