牛客网 sql1-10
2020-04-20 本文已影响0人
jiaway
- 查找最晚入职员工的所有信息
-- 我的答案
select * from employees order by hire_date desc limit 1;
-- 讨论区答案
select * from employees
where hire_date =
(select max(hire_date) from employees)
2.查找入职员工时间排名倒数第三的员工所有信息
-- 我的答案
select * from employees order by hire_date desc limit 2,1
-- 讨论区答案
select * from employees
where hire_date = (
select distinct hire_date from employees order by hire_date desc limit 2,1
)
3.查找各个部门当前(to_date='9999-01-01')领导当前薪水详情以及其对应部门编号dept_no
select s.*,d.dept_no from salaries s , dept_manager d
on d.emp_no=s.emp_no and d.to_date='9999-01-01' and s.to_date=d.to_date
4.查找所有已经分配部门的员工的last_name和first_name以及dept_no
select e.last_name,e.first_name,d.dept_no from dept_emp d,employees e on e.emp_no = d.emp_no
5.查找所有员工的last_name和first_name以及对应部门编号dept_no,也包括展示没有分配具体部门的员工
select e.last_name,e.first_name,d.dept_no from employees e left join dept_emp d on e.emp_no = d.emp_no
6.查找所有员工入职时候的薪水情况,给出emp_no以及salary, 并按照emp_no进行逆序
select e.emp_no,s.salary from employees e,salaries s on e.emp_no=s.emp_no and hire_date=from_date order by s.emp_no desc
7.查找薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数t
-- 我的答案
select emp_no,count(*) from salaries group by emp_no having count(*)>15
-- 讨论区答案
select a.emp_no,count(*) t from salaries a inner join salaries b
on a.emp_no=b.emp_no and a.to_date = b.from_date
where a.salary < b.salary
group by a.emp_no
having t>``15
- 找出所有员工当前(to_date='9999-01-01')具体的薪水salary情况,对于相同的薪水只显示一次,并按照逆序显示
-- 我的答案
select distinct salary from salaries where to_date ='9999-01-01' order by salary desc
-- 讨论区答案 使用分组去重 效率更高
select salary from salaries where to_date='9999-01-01' group by salary order by salary desc
9.获取所有部门当前manager的当前薪水情况,给出dept_no, emp_no以及salary,当前表示to_date='9999-01-01'
select d.dept_no,d.emp_no,s.salary from dept_manager d,salaries s
on d.emp_no=s.emp_no
and d.to_date=s.to_date
and d.to_date='9999-01-01'
10.获取所有非manager的员工emp_no
-- 答案一
SELECT employees.emp_no
FROM employees
EXCEPT
SELECT dept_manager.emp_no
FROM dept_manager;
- 答案二
SELECT emp_no FROM employees
WHERE emp_no NOT IN (SELECT emp_no FROM dept_manager)