21.
salaries
( emp_no
int(11) NOT NULL, salary
int(11) NOT NULL, from_date
date NOT NULL, to_date
date NOT NULL, emp_no
,from_date
));SELECT s1.emp_no, s1.salary, COUNT(DISTINCT s2.salary) AS rank
FROM salaries AS s1, salaries AS 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, s1.emp_no ASC
22.
dept_emp
( emp_no
int(11) NOT NULL, dept_no
char(4) NOT NULL, from_date
date NOT NULL, to_date
date NOT NULL, emp_no
,dept_no
)); dept_manager
( dept_no
char(4) NOT NULL, emp_no
int(11) NOT NULL, from_date
date NOT NULL, to_date
date NOT NULL, emp_no
,dept_no
)); 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, emp_no
)); salaries
( emp_no
int(11) NOT NULL, salary
int(11) NOT NULL, from_date
date NOT NULL, to_date
date NOT NULL, emp_no
,from_date
));SELECT de.dept_no, s.emp_no, s.salary
FROM (employees AS e JOIN salaries AS s ON s.emp_no = e.emp_no AND s.to_date = '9999-01-01')
JOIN dept_emp AS de ON e.emp_no = de.emp_no
WHERE de.emp_no NOT IN (SELECT emp_no FROM dept_manager WHERE to_date = '9999-01-01')
23.
dept_emp
( emp_no
int(11) NOT NULL, dept_no
char(4) NOT NULL, from_date
date NOT NULL, to_date
date NOT NULL, emp_no
,dept_no
)); dept_manager
( dept_no
char(4) NOT NULL, emp_no
int(11) NOT NULL, from_date
date NOT NULL, to_date
date NOT NULL, emp_no
,dept_no
)); salaries
( emp_no
int(11) NOT NULL, salary
int(11) NOT NULL, from_date
date NOT NULL, to_date
date NOT NULL, emp_no
,from_date
));SELECT sem.emp_no AS emp_no, sdm.emp_no AS manager_no, sem.salary AS emp_salary, sdm.salary AS manager_salary
FROM (SELECT s.salary, s.emp_no, de.dept_no FROM salaries s INNER JOIN dept_emp de
ON s.emp_no = de.emp_no AND s.to_date = '9999-01-01' ) AS sem,
(SELECT s.salary, s.emp_no, dm.dept_no FROM salaries s INNER JOIN dept_manager dm
ON s.emp_no = dm.emp_no AND s.to_date = '9999-01-01' ) AS sdm
WHERE sem.dept_no = sdm.dept_no AND sem.salary > sdm.salary
24.
题目描述
汇总各个部门当前员工的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);
题目解答
1、先用 INNER JOIN 连接 dept_emp 与 salaries,根据测试数据添加限定条件 de.to_date = ‘9999-01-01’ AND t.to_date = ‘9999-01-01’,即当前员工的当前头衔
2、再用 INNER JOIN 连接departments,限定条件为 de.dept_no = dp.dept_no,即部门编号相同
3、最后用 GROUP BY 同时对 de.dept_no 和 t.title 进行分组,用 COUNT(t.title) 统计相同部门下相同头衔的员工个数
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
25.
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 s2.emp_no, s2.from_date, (s2.salary - s1.salary) AS salary_growth
FROM salaries AS s1, salaries AS s2
WHERE s1.emp_no = s2.emp_no
AND salary_growth > 5000
AND (strftime("%Y",s2.to_date) - strftime("%Y",s1.to_date) = 1
OR strftime("%Y",s2.from_date) - strftime("%Y",s1.from_date) = 1 )
ORDER BY salary_growth DESC
26.
字段 说明
film_id 电影id
title 电影名称
description 电影描述信息
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));
category表
字段 说明
category_id 电影分类id
name 电影分类名称
last_update 电影分类最后更新时间
CREATE TABLE category (
category_id tinyint(3) NOT NULL ,
name varchar(25) NOT NULL, last_update
timestamp,
PRIMARY KEY ( category_id ));
film_category表
字段 说明
film_id 电影id
category_id 电影分类id
last_update 电影id和分类id对应关系的最后更新时间
CREATE TABLE film_category (
film_id smallint(5) NOT NULL,
category_id tinyint(3) NOT NULL, last_update
timestamp);
查找描述信息中包括robot的电影对应的分类名称以及电影数目,而且还需要该分类对应电影数量>=5部
select category.name,count(film.film_id)
from ((category join film_category on category.category_id=film_category.category_id)as temple
join film on temple.film_id=film.film_id)as tt
join (select category_id, COUNT(film_id) AS category_num FROM
film_category GROUP BY category_id HAVING count(film_id)>=5) AS cc on tt.category_id=cc.category_id
where film.description LIKE '%robot%'
27.
SELECT f.film_id, f.title FROM film f LEFT JOIN film_category fc
ON f.film_id = fc.film_id WHERE fc.category_id IS NULL
28.
select f.title,f.description
from film f
where f.film_id in
(select fc.film_id from film_category fc join category c
on fc.category_id=c.category_id and c.name='Action')
29.
EXPLAIN SELECT * FROM employees
30.
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, emp_no
));SELECT last_name||" "||first_name AS Name FROM employees