SQL练习题笔记
查找最晚入职员工的所有信息
select * from employees order by hire_date desc limit 1
查找入职员工时间排名倒数第三的员工所有信息
select * from employees order by hire_date desc limit 2,1
查找当前薪水详情以及部门编号dept_no
select salaries.*,dept_manager.dept_no from salaries,dept_manager where dept_manager.emp_no = salaries.emp_no
and dept_manager.to_date = "9999-01-01" and salaries.to_date = "9999-01-01"
查找所有已经分配部门的员工的last_name和first_name和部门
select employees.last_name,employees.first_name,dept_emp.dept_no from employees,dept_emp where dept_emp.emp_no = employees.emp_no
查找所有员工的last_name和first_name以及对应部门编号dept_no,也包括展示没有分配具体部门的员工
select employees.last_name,employees.first_name,dept_emp.dept_no from employees left join dept_emp on
dept_emp.emp_no = employees.emp_no
查找所有员工入职时候的薪水情况,给出emp_no以及salary, 并按照emp_no进行逆序
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
查找薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数t
select emp_no,count(*) as tmp from salaries group by emp_no having tmp > 15
找出所有员工当前(to_date='9999-01-01')具体的薪水salary情况,对于相同的薪水只显示一次,并按照逆序显示
select distinct salary from salaries where to_date='9999-01-01' order by salary desc
获取所有部门当前manager的当前薪水情况,给出dept_no, emp_no以及salary,当前表示to_date='9999-01-01'
select dept_manager.dept_no,dept_manager.emp_no,salaries.salary from salaries join dept_manager
on dept_manager.emp_no = salaries.emp_no and dept_manager.to_date='9999-01-01' and salaries.to_date='9999-01-01'
获取所有非manager的员工emp_no
select emp_no from employees where emp_no not in (select emp_no from dept_manager)
获取所有部门中当前员工薪水最高的相关信息,给出dept_no, emp_no以及其对应的salary
select a.dept_no,a.emp_no,max(b.salary) from salaries b inner join dept_emp a on a.emp_no = b.emp_no
where a.to_date = '9999-01-01' and b.to_date = '9999-01-01'
group by a.dept_no
从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t
select distinct title,count(emp_no) as t from titles group by title having t >= 2
从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。
注意对于重复的emp_no进行忽略。
select title,count(distinct emp_no) as t from titles group by title having t >= 2
查找employees表所有emp_no为奇数,且last_name不为Mary的员工信息,并按照hire_date逆序排列
select * from employees where last_name != 'Mary' and
round(emp_no/2.0) != emp_no/2 order by hire_date desc
统计出当前各个title类型对应的员工当前薪水对应的平均工资。结果给出title以及平均工资avg。
select a.title,avg(b.salary) as avg from titles a,salaries b where a.emp_no = b.emp_no
and a.to_date = '9999-01-01' and b.to_date = '9999-01-01' group by title
获取当前(to_date='9999-01-01')薪水第二多的员工的emp_no以及其对应的薪水salary
select emp_no,salary from salaries order by salary desc limit 1,1