https://www.nowcoder.com/practice/fc7344ece7294b9e98401826b94c6ea5?tpId=82&tqId=29773&rp=0&ru=/ta/sql&qru=/ta/sql/question-ranking
查找所有员工自入职以来的薪水涨幅情况,给出员工编号emp_noy以及其对应的薪水涨幅growth,并按照growth进行升序
CREATE TABLE employees
(
emp_no
int(11) NOT NULL,
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 a.emp_no,(b.salary-a.salary) as growth
from (select e.emp_no,s.salary
from salaries s,employees e
where s.emp_no=e.emp_no
and s.from_date=e.hire_date) as a,
(select e.emp_no,s.salary
from salaries s,employees e
where s.emp_no=e.emp_no
and s.to_date='9999-01-01') as b
where a.emp_no=b.emp_no
order by growth