牛客网数据库SQL实战(6-10)

时间:2022-09-13 23:49:50

6、查找所有员工入职时候的薪水情况,给出emp_no以及salary, 并按照emp_no进行逆序
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`));

牛客网数据库SQL实战(6-10)

select employees.emp_no , salaries.salary 
from employees
join salaries
on employees.emp_no = salaries.emp_no
and employees.hire_date = salaries.from_date
order by employees.emp_no desc;

select employees.emp_no , salaries.salary 
from employees , salaries
where employees.emp_no = salaries.emp_no
and employees.hire_date = salaries.from_date
order by employees.emp_no desc;

讨论:https://www.nowcoder.com/questionTerminal/23142e7a23e4480781a3b978b5e0f33a

7、查找薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数t
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`));

牛客网数据库SQL实战(6-10)

select emp_no , count(emp_no) as t
from salaries
group by emp_no having t > 15 ;

讨论:https://www.nowcoder.com/questionTerminal/6d4a4cff1d58495182f536c548fee1ae

8、找出所有员工当前(to_date='9999-01-01')具体的薪水salary情况,对于相同的薪水只显示一次,并按照逆序显示
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`));

牛客网数据库SQL实战(6-10)

select salary
from salaries
where to_date = '9999-01-01'
group by salary
order by salary desc;

select DISTINCT salary
from salaries
where to_date = '9999-01-01'
order by salary desc;

讨论:https://www.nowcoder.com/questionTerminal/ae51e6d057c94f6d891735a48d1c2397

9、获取所有部门当前manager的当前薪水情况,给出dept_no, emp_no以及salary,当前表示to_date='9999-01-01'
CREATE TABLE `dept_manager` (
`dept_no` char(4) NOT NULL,
`emp_no` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_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`));

牛客网数据库SQL实战(6-10)

select d.dept_no,s.emp_no,s.salary
from dept_manager as d
join salaries as s
on d.emp_no = s.emp_no
and d.to_date = '9999-01-01'
and s.to_date = '9999-01-01';

select d.dept_no,s.emp_no,s.salary
from dept_manager as d
join salaries as s
on d.emp_no = s.emp_no
and d.to_date = '9999-01-01'
and s.to_date = '9999-01-01'
order by d.emp_no desc;

讨论:https://www.nowcoder.com/questionTerminal/4c8b4a10ca5b44189e411107e1d8bec1

10、获取所有非manager的员工emp_no
CREATE TABLE `dept_manager` (
`dept_no` char(4) NOT NULL,
`emp_no` int(11) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
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`));

牛客网数据库SQL实战(6-10)

select e.emp_no
from employees e 
left join dept_manager d
on e.emp_no = d.emp_no
where d.emp_no is null;

select emp_no
from employees
where emp_no not in
(select emp_no from dept_manager);

select emp_no
from ( select * from employees 
      left join dept_manager 
      on employees.emp_no = dept_manager.emp_no)
where dept_no is null;

讨论: https://www.nowcoder.com/questionTerminal/32c53d06443346f4a2f2ca733c19660c