61 Star 426 Fork 144

xuthus / 数据库SQL实战

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
12.获取所有部门中当前员工薪水最高的相关信息.md 1.05 KB
一键复制 编辑 原始数据 按行查看 历史

获取所有部门中当前员工薪水最高的相关信息

题目描述

获取所有部门中当前员工薪水最高的相关信息,给出dept_no, emp_no以及其对应的salary

CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE `salaries` (
`emp_no` int(11) NOT NULL,
`salary` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`from_date`));

答案

select dept_no,s.emp_no,max(salary) salary from dept_emp de left join salaries s on de.emp_no = s.emp_no where de.to_date = '9999-01-01' and s.to_date = '9999-01-01' group by dept_no having s.salary = max(salary)

题解

select a.dept_no as '部门编号',a.emp_no as '员工编号', max(a.salary) as '薪资' from (select dp.dept_no,dp.emp_no,SS.salary from dept_emp dp left join salaries ss on dp.emp_no = ss.emp_no where dp.to_date = '9999-01-01' and ss.to_date = '9999-01-01' ) a group by a.dept_no

SQL
1
https://gitee.com/xuthus5/Database-SQL-Actual-Combat.git
git@gitee.com:xuthus5/Database-SQL-Actual-Combat.git
xuthus5
Database-SQL-Actual-Combat
数据库SQL实战
master

搜索帮助