20.查詢員工編號emp_no為10001其自入職以來的薪水salary漲幅值growth

weixin_34290000發表於2018-07-08

查詢員工編號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

相關文章