問題: 求部門工資最高的員工
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 |
解答:
mysql>select d.name as department,b.name as employee,b.salary from (select e.*
from employee e inner
join
-> (select
max(salary) as max_salary,departmentid from employee group
bydepartmentid) a
->
on e.salary=a.max_salary and e.departmentid=a.departmentid) b left
join
department d
->
on b.departmentid=
d.id ;+--
----------+----------+--------+
| department | employee | salary |+--
----------+----------+--------+
| sales | henry |
80000
|| it |
max|
90000|+
------------+----------+--------+
2 rows in
set (0.00 sec)
過程:
createtable
employee(
id int,
name
varchar(20) not
null
, salary
int,
departmentid
intnot
null
,
primary
key(id))
insert
into employee(id,name,salary,departmentid) values ('
1','
joe','
70000
','1');
insert
into employee(id,name,salary,departmentid) values ('
2','
henry
','80000
','2');
insert
into employee(id,name,salary,departmentid) values ('
3','
sam','
60000
','2');
insert
into employee(id,name,salary,departmentid) values ('
4','
max','
90000
','1');
create
table
department(
id int,
name
varchar(20) not
null
,
primary
key(id))
insert
into department(id,name) values('
1','it'
);insert
into department(id,name) values('
2','
sales');
select
max(salary) as max_salary,departmentid from employee group
bydepartmentid
select e.*
from employee e inner
join
(select
max(salary) as max_salary,departmentid from employee group
bydepartmentid) a
on e.salary=a.max_salary and e.departmentid=
a.departmentid
select b.id,b.name,b.salary,d.name from (select e.*
from employee e inner
join
(select
max(salary) as max_salary,departmentid from employee group
bydepartmentid) a
on e.salary=a.max_salary and e.departmentid=a.departmentid) b left
join
department d
on b.departmentid=
d.id
select d.name as department,b.name as employee,b.salary from (select e.*
from employee e inner
join
(select
max(salary) as max_salary,departmentid from employee group
bydepartmentid) a
on e.salary=a.max_salary and e.departmentid=a.departmentid) b left
join
department d
on b.departmentid=d.id
--2023年5月10日 17點22分--
1.
部門工資最高的員工
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 |
+------------+----------+--------+
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...
部門工資最高的員工
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...