1、获取所有非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
));
select d.dept_no , e.emp_no , s.salary
from dept_emp d , employees e , salaries s
where d.emp_no = e.emp_no
and s.emp_no = e.emp_no
and d.to_date = '9999-01-01'
and s.to_date = '9999-01-01'
and e.emp_no not in (select emp_no from dept_manager)
2、获取员工其当前的薪水比其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
));
select EmpInfo.emp_no as emp_no, ManagerInfo.emp_no as manager_no , EmpInfo.salary as emp_salary, ManagerInfo.salary as manager_salary
from (select de.emp_no , s.salary , de.dept_no
from dept_emp de , salaries s , dept_manager dp
where de.emp_no = s.emp_no
and dp.dept_no = de.dept_no
and s.to_date = '9999-01-01'
and de.emp_no not in (select emp_no from dept_manager)) EmpInfo , (select de.emp_no , sa.salary , de.dept_no
from dept_manager de , salaries sa
where sa.to_date = '9999-01-01'
and de.emp_no = sa.emp_no) as ManagerInfo
where EmpInfo.dept_no = ManagerInfo.dept_no
and EmpInfo.salary > ManagerInfo.salary
3、汇总各个部门当前员工的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);
/*
SELECT de.dept_no, dp.dept_name, t.title, COUNT(t.title) AS count
FROM titles AS t INNER JOIN dept_emp AS de
ON t.emp_no = de.emp_no AND de.to_date = '9999-01-01' AND t.to_date = '9999-01-01'
INNER JOIN departments AS dp
ON de.dept_no = dp.dept_no
GROUP BY de.dept_no, t.title
*/
SELECT c.dept_no, dp.dept_name, c.title, COUNT(c.title) AS count
FROM (select de.dept_no ,t.title
from titles AS t,dept_emp AS de
where t.emp_no = de.emp_no AND de.to_date = '9999-01-01' AND t.to_date = '9999-01-01') as c , departments AS dp
where c.dept_no = dp.dept_no
GROUP BY dp.dept_no, c.title
4、给出每个员工每年薪水涨幅超过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
));
-- (strftime('%Y',s2.to_date) - strftime('%Y',s1.to_date) = 1
-- or strftime('%Y',s2.from_date) - strftime('%Y',s1.from_date) = 1
-- 一年可能涨两次薪水 也可能两年涨一次薪水
-- strftime('%Y',s2.to_date) - strftime('%Y',s1.from_date) = 1
select s1.emp_no , s2.from_date ,(s2.salary - s1.salary) as salary_growth
from salaries s1 , salaries s2
where (strftime('%Y',s2.to_date) - strftime('%Y',s1.to_date) = 1
or strftime('%Y',s2.from_date) - strftime('%Y',s1.from_date) = 1 )
and s1.emp_no = s2.emp_no
and salary_growth > 5000
order by salary_growth desc;
5、查找描述信息中包括robot的电影对应的分类名称以及电影数目,而且还需要该分类对应电影数量>=5部
CREATE TABLE IF NOT EXISTS film (
film_id smallint(5) NOT NULL DEFAULT ‘0’,
title varchar(255) NOT NULL,
description text,
PRIMARY KEY (film_id));
CREATE TABLE category (
category_id tinyint(3) NOT NULL ,
name varchar(25) NOT NULL, last_update
timestamp,
PRIMARY KEY ( category_id ));
CREATE TABLE film_category (
film_id smallint(5) NOT NULL,
category_id tinyint(3) NOT NULL, last_update
timestamp);
last_update 电影id和分类id对应关系的最后更新时间
category_id 电影分类id
last_update 电影分类最后更新时间
description 电影描述信息
/*
查找描述信息中包括robot的电影对应的分类名称以及电影数目,而且还需要该分类对应电影数量>=5部
*/
select c.name, count(f.film_id) as number
from film_category as fc ,category as c ,film as f
where f.description like '%robot%'
and f.film_id = fc.film_id
and fc.category_id = c.category_id
and c.category_id in (select category_id
from film_category
group by category_id
having count(film_id) >= 5)
-- 先查询出电影数量大于5的分类的集合,然后限定category必须在该集合中
6、使用join查询方式找出没有分类的电影id以及名称(见第五题表)
-- 使用join查询方式找出没有分类的电影id以及名称
select f.film_id , f.title
from film f left join film_category fc
on f.film_id = fc.film_id
left join category ca
on ca.category_id = fc.category_id
where f.film_id not in (select film_id from film_category)
-- 没有分类用 is null就可以了 但是为什么我写 not in (select category_id from category )
7、使用子查询的方式找出属于Action分类的所有电影对应的title,description(见第五题表)
-- 使用子查询的方式找出属于Action分类的所有电影对应的title,description
select f.title , f.description
from film f, film_category ca
where f.film_id = ca.film_id
and ca.category_id in (select category_id
from category
where name = 'Action')
如有书写错误 或者有其他书写意见,欢迎交流,谢谢观看!