员工表结构:
部门表结构:
部门员工表结构:
部门经理表结构:
薪资表结构:
1.查找所有员工自入职以来的薪水涨幅情况,给出员工编号emp_no以及其对应的薪水涨幅growth,并按照growth进行升序
分析:分别找出每个员工入职时的薪资即e.hire_date=s.from_date,每个员工当前的薪资即e.to_date=s.to_date,join关联找出每个emp_no对应的growth。
select r1.emp_no, (r1.salary-r2.salary) as growth from
(select emp_no,salary from salaries where to_date='9999-01-01')r1 #每位员工当前薪水
inner join
(select e.emp_no,salary from employees e inner join salaries s on e.emp_no=s.emp_no and e.hire_date=s.from_date)r2 #每位员工入职时薪水
on r1.emp_no=r2.emp_no
order by growth;
2.统计各个部门的工资记录数,给出部门编码dept_no、部门名称dept_name以及次数sum
分析:多表联合查询,对部门编号分组,过滤表之间的关系。
select de.dept_no, d.dept_name, count(salary) sum from salaries s, dept_emp de, departments d
where s.emp_no = de.emp_no and de.dept_no = d.dept_no
group by de.dept_no;
3.对所有员工的当前(to_date='9999-01-01')薪水按照salary进行按照1-N的排名,相同salary并列且按照emp_no升序排列
分析:考察自查询,限制条件to_date='9999-01-01',a.salary<=b.salary意思时在输出a.salary时,有多少b.salary大于等于a.salary。由于用了count()函数,需要对a.emp_no 分组group by。
select a.emp_no,a.salary,count(distinct b.salary) rank from salaries a,salaries b
where a.to_date='9999-01-01'
and b.to_date='9999-01-01'
and a.salary<=b.salary
group by a.emp_no
order by a.salary desc,a.emp_no asc;
4.获取所有非manager员工当前的薪水情况,给出dept_no、emp_no以及salary
分析:首先找出非manager员工,连接salaries表找到对应的当前薪水,再连接dept_emp找到员工对应的部门
select de.dept_no, e.emp_no, s.salary from employees e
inner join salaries s on e.emp_no = s.emp_no and s.to_date = '9999-01-01'
inner join dept_emp de on de.emp_no = e.emp_no
where e.emp_no not in (select emp_no from dept_manager where to_date = '9999-01-01');
5.获取员工其当前的薪水比其manager当前薪水还高的相关信息,当前表示to_date='9999-01-01'
结果第一列给出员工的emp_no,
第二列给出其manager的manager_no,
第三列给出该员工当前的薪水emp_salary,
第四列给该员工对应的manager当前的薪水manager_salary
分析:分别找出各部门员工的薪水,各部门经理的薪水,限制条件为同一部门其员工薪水>经理薪水,按要求查询4列
select r1.emp_no emp_no, r2.emp_no manager_no, r1.salary emp_salary, r2.salary manager_salary from
(select de.emp_no,de.dept_no,s.salary from dept_emp de inner join salaries s on de.emp_no=s.emp_no and s.to_date='9999-01-01') r1,
(select dm.emp_no,dm.dept_no,s.salary from dept_manager dm inner join salaries s on dm.emp_no=s.emp_no and s.to_date='9999-01-01') r2
where r1.dept_no = r2.dept_no and r1.salary > r2.salary;
6.汇总各个部门当前员工的title类型的分配数目,结果给出部门编号dept_no、dept_name、其当前员工所有的title以及该类型title对应的数目count
select r.dept_no,d.dept_name,r.title,r.count from departments d inner join
(select de.dept_no,t.title,count(*) count from dept_emp de,titles t where de.emp_no=t.emp_no
and de.to_date='9999-01-01' and t.to_date='9999-01-01' group by de.dept_no,title)r
on d.dept_no=r.dept_no;
电影表结构:
分类表结构:
电影分类表结构:
7.查找描述信息中包括robot的电影对应的分类名称以及电影数目,而且还需要该分类对应电影数量>=5部
SELECT c.name AS name, COUNT(f.film_id) AS amount
FROM film AS f, film_category AS fc, category AS c,
(SELECT category_id FROM film_category GROUP BY category_id HAVING COUNT(category_id) >= 5) AS cc
WHERE f.description LIKE '%robot%'
AND f.film_id = fc.film_id
AND fc.category_id = c.category_id
AND c.category_id = cc.category_id;
8.使用join查询方式找出没有分类的电影id以及名称
select film_id,title from film where film_id not in
(select film_id from film_category fc inner join category c on fc.category_id=c.category_id);
9.使用子查询的方式找出属于Action分类的所有电影对应的title,description
select f.title,f.description from film f where f.film_id in
(select fc.film_id from film_category fc where fc.category_id in
(select c.category_id from category c where c.name='Action'));
10.获取select * from employees对应的执行计划
explain select * from employees;