LeetCode SQL 部門工資最高的員工

2021-10-06 06:06:47 字數 1352 閱讀 9587

employee 表包含所有員工資訊,每個員工有其對應的 id, salary 和 department id。

+----+-------+--------+--------------+

| id | name  | salary | departmentid |

+----+-------+--------+--------------+

| 1  | joe   | 70000  | 1            |

| 2  | henry | 80000  | 2            |

| 3  | sam   | 60000  | 2            |

| 4  | max   | 90000  | 1            |

+----+-------+--------+--------------+

department 表包含公司所有部門的資訊。

+----+----------+

| id | name     |

+----+----------+

| 1  | it       |

| 2  | sales    |

+----+----------+

編寫乙個 sql 查詢,找出每個部門工資最高的員工。例如,根據上述給定的**,max 在 it 部門有最高工資,henry 在 sales 部門有最高工資。

+------------+----------+--------+

| department | employee | salary |

+------------+----------+--------+

| it         | max      | 90000  |

| sales      | henry    | 80000  |

+------------+----------+--------+

select

department.name as 'department',

employee.name as 'employee',

salary

from

employee

join

department on employee.departmentid = department.id

where

(employee.departmentid , salary) in

( select

departmentid, max(salary)

from

employee

group by departmentid

);

部門工資最高的員工

employee表包含所有員工資訊,每個員工有其對應的 id,salary 和 department id。id name salary departmentid 1 joe 70000 1 2 henry 80000 2 3 sam 60000 2 4 max 90000 1 department...

部門工資最高的員工

leecode的題目。關於in的應用。感覺很經典,這裡列出解題過程。employee 表包含所有員工資訊,每個員工有其對應的 id,salary 和 department id。sql如下 set names utf8mb4 set foreign key checks 0 table struct...

leetcode 求部門工資最高的員工

employee表包含所有員工資訊,每個員工有其對應的 id,salary 和 department id。id name salary departmentid 1 joe 70000 1 2 henry 80000 2 3 sam 60000 2 4 max 90000 1 department...