20.查找员工编号emp_no为10001其自入职以来的薪水salary涨幅值growth

查找员工编号emp_no为10001其自入职以来的薪水salary涨幅值growth

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`));

解析:考察排序和limit
解法一:最大值减去最小值,稍微有点不严谨

select (max(salary) - min(salary)) as growth
from salaries
where emp_no = 10001;

解法二:

select
(select salary from salaries where emp_no = 10001 order by to_date desc limit 0,1) - 
(select salary from salaries where emp_no = 10001 order by to_date asc limit 0,1) as growth

你可能感兴趣的:(20.查找员工编号emp_no为10001其自入职以来的薪水salary涨幅值growth)