Leecode SQL 184. Department Highest Salary 找出tie

  1. Department Highest Salary

注意!要找出 tie 的 highest salary!

Write a solution to find employees who have the highest salary in each of the departments.

Return the result table in any order.

The result format is in the following example.

Input
Employee =

idnamesalarydepartmentId
1Joe700001
2Jim900001
3Henry800002
4Sam600002
5Max900001

Department =

idname
1IT
2Sales

Output

DepartmentEmployeesalary
ITJim90000
SalesHenry80000
ITMax90000

My wrong solution (didn’t find the tie):

-- WITH a AS (
--     SELECT departmentId AS di, name, MAX(salary) AS highest
--     FROM employee e
--     GROUP BY e.departmentId
-- )

-- SELECT d.name AS Department, a.name AS Employee, a.highest AS Salary
-- FROM a
--     JOIN department d
--         ON a.di = d.id

Correct solution by others:

SELECT dp.name AS Department, em.name AS Employee, em.salary
FROM Employee AS em 
    JOIN Department as dp 
        ON em.departmentId = dp.id 
WHERE em.salary = (SELECT MAX(salary) FROM Employee 
				WHERE departmentId = dp.id )
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZeroSnowy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值