【牛客sql實戰12】獲取所有部門中當前員工薪水最高的相關資訊(親測通過)
阿新 • • 發佈:2021-01-25
題目描述
獲取所有部門中當前(dept_emp.to_date = ‘9999-01-01’)員工當前(salaries.to_date=‘9999-01-01’)薪水最高的相關資訊,給出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 d.dept_no, d.emp_no, s.salary
from dept_emp as d
inner join salaries as s on d.emp_no=s.emp_no
and d. to_date='9999-01-01'
and s.to_date='9999-01-01'
where s.salary in
(select max(s1.salary)
from dept_emp as d1
inner join salaries as s1 on d1.emp_no=s1.emp_no
and d1.to_date='9999-01-01'
and s1.to_date='9999-01-01'
and d1.dept_no = d.dept_no
)
order by d.dept_no;