数据库SQL实战(牛客网):查找所有员工自入职以来的薪水涨幅情况

查找所有员工自入职以来的薪水涨幅情况,给出员工编号emp_no以及其对应的薪水涨幅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));

三表相连,b和c的salary分别代表当前薪资和入职薪资

select a.emp_no , (b.salary - c.salary) as growth
from employees as a 
join salaries as b
on a.emp_no = b.emp_no and b.to_date = '9999-01-01'
join salaries as c 
on a.emp_no = c.emp_no and a.hire_date = c.from_date
order by growth asc;

发布了292 篇原创文章 · 获赞 73 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43777983/article/details/104486491