目录
21. 查找所有员工自入职以来的薪水涨幅情况(多表)
22. 统计各个部门对应员工涨幅的次数总和(多表)
23. 对所有员工的薪水按照salary进行按照1-N的排名(单表)
24. 获取所有非manager员工当前的薪水情况(多表)
25. 获取员工其当前的薪水比其manager当前薪水还高的相关信息(多表)
26. 汇总各个部门当前员工的title类型的分配数目(多表)
27. 给出每个员工每年薪水涨幅超过5000的员工编号emp_no(单表)
28. 查找描述信息中包括robot的电影对应的分类名称以及电影数目,而且还需要该分类(多表)
29. 使用join查询方式找出没有分类的电影id以及名称(单表)
30. 使用子查询的方式找出属于Action分类的所有电影对应的title,description
查找所有员工自入职以来的薪水涨幅情况,给出员工编号emp_no以及其对应的薪水涨幅growth,并按照growth进行升序
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`));
解决思路1:
用inner join或者left join的方法,生成一张现在的工资表和一张入职时的工资表。
SELECT sCurrent.emp_no, (sCurrent.salary-sStart.salary) AS growth
FROM (SELECT s.emp_no, s.salary FROM employees e LEFT JOIN salaries s ON e.emp_no = s.emp_no WHERE s.to_date = '9999-01-01') AS sCurrent
INNER JOIN (SELECT s.emp_no, s.salary FROM employees e LEFT JOIN salaries s ON e.emp_no = s.emp_no WHERE s.from_date = e.hire_date) AS sStart
ON sCurrent.emp_no = sStart.emp_no
ORDER BY growth
解决思路2:
用where的方法判断,生成现在的工资表和入职时的工资表。
SELECT sCurrent.emp_no, (sCurrent.salary-sStart.salary) AS growth
FROM (SELECT s.emp_no, s.salary FROM employees e, salaries s WHERE e.emp_no = s.emp_no AND s.to_date = '9999-01-01') AS sCurrent,
(SELECT s.emp_no, s.salary FROM employees e, salaries s WHERE e.emp_no = s.emp_no AND s.from_date = e.hire_date) AS sStart
WHERE sCurrent.emp_no = sStart.emp_no
ORDER BY growth
统计各个部门对应员工涨幅的次数总和,给出部门编码dept_no、部门名称dept_name以及次数sum
CREATE TABLE `departments` (
`dept_no` char(4) NOT NULL,
`dept_name` varchar(40) NOT NULL,
PRIMARY KEY (`dept_no`));
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) 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`));
解决思路:
统计salary的记录数,作为涨幅次数。然后用left join或inner join的方法进行多表关联。
select de.dept_no, d.dept_name, count(s.salary) as sum
from dept_emp de
left join salaries s on de.emp_no = s.emp_no
left join departments d on de.dept_no = d.dept_no
group by de.dept_no;
对所有员工的当前(to_date='9999-01-01')薪水按照salary进行按照1-N的排名,相同salary并列且按照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`));
解决思路:
复用salaries表进行比较排名。s1.salary <= s2.salary,意思是在输出s1.salary的情况下,有多少个s2.salary大于等于s1.salary,比如当s1.salary=10000时,有3个s2.salary(分别为12000,10000,10000)大于等于它,但由于10000和s1里的数据是重复使用的,利用COUNT(DISTINCT s2.salary)去重,可得工资为10000的rank等于2。
select s1.emp_no, s1.salary, count(distinct s2.salary) as rank
from salaries s1, salaries s2
where s1.to_date='9999-01-01'
and s2.to_date='9999-01-01'
and s1.salary <= s2.salary
group by s1.emp_no
order by s1.salary desc, s2.emp_no asc;
获取所有非manager员工当前的薪水情况,给出dept_no、emp_no以及salary ,当前表示to_date='9999-01-01'
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_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`));
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`));
解决思路:
利用left join进行多表关联,使用not null筛选出非manager的员工。
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
left join dept_manager dm on e.emp_no = dm.emp_no
where dm.dept_no is null;
获取员工其当前的薪水比其manager当前薪水还高的相关信息,当前表示to_date='9999-01-01',
结果第一列给出员工的emp_no,
第二列给出其manager的manager_no,
第三列给出该员工当前的薪水emp_salary,
第四列给该员工对应的manager当前的薪水manager_salary
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_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 `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`));
解决思路:
创建一张员工的工资表,一张manager的工资表,最后判断薪水比magager多的员工。
select de.emp_no as emp_no, dm.emp_no as manager_no, de.salary as emp_salary, dm.salary as manager_salary
from (select e.dept_no, e.emp_no, s.salary
from dept_emp e
inner join salaries s on e.emp_no = s.emp_no and s.to_date='9999-01-01') as de
inner join (select m.dept_no, m.emp_no, s.salary from dept_manager m
inner join salaries s on m.emp_no = s.emp_no and s.to_date='9999-01-01')
as dm on de.dept_no = dm.dept_no
where emp_salary > manager_salary and de.dept_no = dm.dept_no;
汇总各个部门当前员工的title类型的分配数目,结果给出部门编号dept_no、dept_name、其当前员工所有的title以及该类型title对应的数目count
CREATE TABLE `departments` (
`dept_no` char(4) NOT NULL,
`dept_name` varchar(40) NOT NULL,
PRIMARY KEY (`dept_no`));
CREATE TABLE `dept_emp` (
`emp_no` int(11) NOT NULL,
`dept_no` char(4) NOT NULL,
`from_date` date NOT NULL,
`to_date` date NOT NULL,
PRIMARY KEY (`emp_no`,`dept_no`));
CREATE TABLE IF NOT EXISTS `titles` (
`emp_no` int(11) NOT NULL,
`title` varchar(50) NOT NULL,
`from_date` date NOT NULL,
`to_date` date DEFAULT NULL);
解决思路:
用 GROUP BY 同时对 de.dept_no 和 t.title 进行分组。
select d.dept_no, d.dept_name, t.title, count(*) as count
from titles t
inner join dept_emp e on e.emp_no = t.emp_no and e.to_date='9999-01-01' and t.to_date='9999-01-01'
inner join departments d on e.dept_no = d.dept_no
group by d.dept_no, t.title;
给出每个员工每年薪水涨幅超过5000的员工编号emp_no、薪水变更开始日期from_date以及薪水涨幅值salary_growth,并按照salary_growth逆序排列。
提示:在sqlite中获取datetime时间对应的年份函数为strftime('%Y', to_date)
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`));
解决思路:
假设s1是涨薪水前的表,s2是涨薪水后的表,因为每个员工涨薪水的时间不全固定,有可能一年涨两次,有可能两年涨一次,所以每年薪水的涨幅,应该理解为两条薪水记录的from_date相同或to_date相同。
select s2.emp_no, s2.from_date, (s2.salary - s1.salary) as salary_growth
from salaries s1, salaries s2
where s1.emp_no = s2.emp_no
and salary_growth > 5000
and (strftime('%Y', s2.from_date) - strftime('%Y', s1.from_date) = 1 or
strftime('%Y', s2.to_date) - strftime('%Y', s1.to_date) = 1)
order by salary_growth desc;
查找描述信息中包括robot的电影对应的分类名称以及电影数目,而且还需要该分类对应电影数量>=5部
解决思路:
找到对应电影数量>=5的所有分类,建立成虚表cc,然后设定限制条件,最后在表cc、f、fc、c中查找包括robot的电影对应的分类名称和对应的电影数目。
select c.name, count(f.film_id) as count
from film f, film_category fc, category 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
使用join查询方式找出没有分类的电影id以及名称
解决思路:
运用 LEFT JOIN 连接两表,用 IS NULL 语句限定条件。
select f.film_id, f.title
from film f
left join film_category fc on fc.film_id = f.film_id
where fc.category_id is null;
使用子查询的方式找出属于Action分类的所有电影对应的title,description
解决思路:
FROM 三张表查询,同时用 WHERE 限定条件。
select title, description
from film
where film_id in
(select film_id
from film_category
where category_id in
(select category_id from category where name="Action")
);