SQL 部門薪資最高的員工

2021-10-05 11:31:27 字數 1267 閱讀 2895

employee表:

idname

salary

departmentid

1001

emma

12000

11002

alex

13000

21003

tom9000

31004

star

11000

11005

eric

15000

2department表:

idname1it

2sales

3finance

編寫乙個sql語句,找出每個部門薪資最高的員工。

應得查詢結果如下:

department

employee

salary

itemma

12000

sales

eric

15000

finance

tom9000

#分解步驟:

#找到每個部門薪資最高的資訊(表a)

select

max(salary) ms,departmentid from employee group

by departmentid

#找到每個部門最高薪資對應的員工資訊(表b)

select e.name,e.salary,e.departmentid from employee e,a where e.salary=a.ms and e.departmentid=a.departmentid

#找到對應的部門名稱

select d.name department,b.name employee,salary from b , department d where b.departmentid =d.id;

#整合select d.name department,b.name employee,salary from

(select e.name,e.salary,e.departmentid from employee e,

(select

max(salary) m,departmentid from employee group

by departmentid) a

where e.salary =a.m and e.departmentid =a.departmentid) b , department d where

b.departmentid =d.id;

部門工資最高的員工

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 SQL 部門工資最高的員工

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 departmen...