Ron Jeremy
Ron Jeremy

Reputation: 35

Finding the highest paid employee (Salary + Comm) in each department (dID)

Below is my table and I am trying to write a MySQL query that selects the highest paid employee (Salary + Comm) in each department (dID)

EmployeeID  | Name       | mgr | Job         | Salary| Com | HireDate     | dID
1001 | Ron Smith   | 1005| Writer| 90000 | 20000| 20012-04-12| 1
1002 | Ricky Lake  | 1003| Writer| 55000 | 15000| 2013-01-18| 1

This is what I have so far

SELECT dID, MAX(coalesce(Salary+Comm, Salary, Comm)) AS 'TotalPaid'
FROM Employee 
Group By dID

How do I go about allowing for the other columns to be shown? Hope I have explained what I want adequately thanks.

Upvotes: 1

Views: 77

Answers (3)

Shoyeb Sheikh
Shoyeb Sheikh

Reputation: 2866

SELECT
    E2.*, E3.TotalPaid
FROM
    Employee E2
INNER JOIN
    (
    SELECT
        MAX(eID) AS eID,
        dID,
        MAX(
            (
            SELECT
                MAX(
                    COALESCE(Salary + Comm, Salary, Comm)
                )
            FROM
                Employee E1
            WHERE
                E.eiD = E1.eID
        )
        ) AS 'TotalPaid'
    FROM
        Employee E
    GROUP BY
        dID
) AS E3
ON
    E2.eID = E3.eID

Upvotes: 0

vishal
vishal

Reputation: 352

Here's one way:

select e.* from 
Employee e inner join 
(SELECT dID, MAX(coalesce(Salary+Comm, Salary, Comm)) as max_salary  AS 'TotalPaid'
FROM Employee 
Group By dID) d
on d.dID = e .dID 
and coalesce(e.Salary+e.Comm, e.Salary, e.Comm)=d.max_salary

Upvotes: 1

Yash Thakkar
Yash Thakkar

Reputation: 1

SELECT dID, MAX(Salary+Comm) AS 'TotalPaid'
FROM Employee 

This will do the Job

Upvotes: 0

Related Questions