SQL_练习:查找所有员工自入职以来的薪水涨幅情况,给出员工编号emp_no以及其对应的薪水涨幅growth,并按照growth进行升序

CREATE TABLE employees (
birth_date date NOT NULL,
first_name varchar(14) NOT NULL,
last_name varchar(16) NOT NULL,
gender char(1) NOT NULL,
hire_date date NOT NULL,
PRIMARY KEY (emp_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 now.emp_no,(now.salary-th.salary)growth 
from (select emp_no, salary from salaries where to_date='9999-01-01') now, (select e.emp_no, s.salary from employees e ,salaries s where e.emp_no=s.emp_no and e.hire_date=s.from_date) th 
where now.emp_no=th.emp_no order by growth 

思路:
select创建now、then的salary,相减作为growth

猜你喜欢

转载自blog.csdn.net/weixin_42836351/article/details/81294682