【练习链接】:https://www.nowcoder.com/ta/sql
第一部分:01-11
SQL01 查找最晚入职员工的所有信息
# 用ALL select * from employees where hire_date >= all (select hire_date from employees ); # 用LIMIT select * from employees order by hire_date DESC limit 1;
SQL02 查找入职员工时间排名倒数第三的员工所有信
select * from employees order by hire_date desc limit 1 offset 2;
SQL03 查找各个部门当前领导当前薪水详情以及其对应部门编号dept_no
select salaries.*,dept_no from dept_manager left join salaries on salaries.emp_no = dept_manager.emp_no where dept_manager.to_date='9999-01-01' and salaries.to_date='9999-01-01' order by salaries.emp_no;
SQL04 查找所有已经分配部门的员工的last_name和first_name
select last_name,first_name,dept_no from employees join dept_emp on employees.emp_no = dept_emp.emp_no
SQL05 查找所有员工的last_name和first_name以及对应部门编号dept_no
select last_name,first_name,dept_no from employees left join dept_emp on employees.emp_no = dept_emp.emp_no
SQL06 查找所有员工入职时候的薪水情况
select salaries.emp_no,salary from employees left join salaries on employees.emp_no = salaries.emp_no where from_date = hire_date order by emp_no desc;
SQL07 查找薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数t
select emp_no,count(distinct salary) as t from salaries group by emp_no having count(distinct salary) > 15;
SQL08 找出所有员工当前具体的薪水salary情况
select distinct salary from salaries where to_date='9999-01-01' order by salary desc;
SQL09 获取所有部门当前manager的当前薪水情况
select dept_no,salaries.emp_no,salary from salaries left join dept_manager on dept_manager.emp_no = salaries.emp_no where dept_manager.to_date='9999-01-01' and salaries.to_date='9999-01-01' order by dept_no;
SQL10 获取所有非manager的员工emp_no
select employees.emp_no from employees left join dept_manager on employees.emp_no = dept_manager.emp_no where employees.emp_no not in (select emp_no from dept_manager);
SQL11 获取所有员工当前的manager
# 如果自己是manager的话不显示 # 相当于查询出自己不是manager的员工和其manager的编号 select dept_emp.emp_no,dept_manager.emp_no from dept_emp left join dept_manager on dept_emp.dept_no = dept_manager.dept_no where dept_manager.to_date='9999-01-01' and dept_emp.emp_no != dept_manager.emp_no