I have the following table:
+---------------------------------------------------+
| ID EMPNAME DEPARTMENT ROLE SALARY EXTRA |
| 1 brian 10 sales 2000 500 |
| 2 jenny 10 leader 3000 100 |
| 3 sam 20 office 1500 500 |
| 4 cory 20 leader 1200 300 |
| 5 tifa 30 custservice4000 1000 |
| 6 sammy 30 leader 3000 200 |
| 7 anne 30 sales 4500 200 |
+---------------------------------------------------+
My job is to find the people who make more than their leader (salary + extra) within each role. I will then display the id, empname, and department of that person. I need a way compare the salaries + extra (extra is the bonus they make each year) between roles within each department. I have tried something like the following code but it didn't work, it wasn't calculating the highest vs the leader properly.
SELECT a.id, a.empname, a.department
FROM MyTable a
JOIN (SELECT MAX(Salary + extra) As Highest, Dept FROM MyTable GROUP BY Dept) b
ON a.Dept = b.Dept && (a.Salary + a.extra) = b.Highest
How do I display the first three columns on the criteria that they make more than their leader in their specific department? The above table should output the following below:
+-----------------------+
| ID EMPNAME DEPARTMENT |
| 3 sam 20 |
| 5 tifa 30 |
| 7 anne 30 |
+-----------------------+
This is done in sql developer.
EDIT: Forgot to add that some values of EXTRA may be NULL.