SQL7

 题目描述
查找员工编号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`));

思路:这是一个单表查询,需要找到入职时的薪水和现在的薪水,拿现在的薪水减去入职时的薪水,即可
得到涨幅值。那么如何查到入职时的薪水和现在的薪水呢?用时间排一下序,取第一条记录即可。
故答案如下:
    
    select (
    (select  salary  from salaries where emp_no=10001
    order by from_date desc limit 1 )-(select  salary  from salaries where emp_no=10001 
    order by from_date asc  limit 1 ) ) as growth

posted @ 2020-02-07 19:55  果实1024  阅读(104)  评论(0编辑  收藏  举报