----------------题目---------------------
--1.查找最晚入职员工的所有信息
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`));
--答案:
--子查询
SELECT * FROM employees
WHERE hire_date
= (
SELECT MAX(hire_date)
FROM employees);
--排序
SELECT * FROM employees
ORDER BY hire_date
DESC LIMIT
0,
1;
--2.查找入职员工时间排名倒数第三的员工所有信息
SELECT * FROM employees
ORDER BY hire_date
DESC LIMIT
2,
1;
--3.查找各个部门当前(to_date=‘9999-01-01‘)领导当前薪水详情以及其对应部门编号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`));
--Answer:
select
salaries.emp_no,salaries.salary,salaries.from_date,salaries.to_date,dept_manager.dept_no
from salaries
inner join dept_manager
on dept_manager.emp_no
= salaries.emp_no
where dept_manager.to_date
= ‘9999-01-01‘
and salaries.to_date
= ‘9999-01-01‘;
--4.查找所有已经分配部门的员工的last_name和first_name
-- 内链接推荐
SELECT e.last_name,e.first_name,d.dept_no
FROM dept_emp d
INNER JOIN employees e
ON d.emp_no
= e.emp_no
--left join(左连接):join左表中所有记录和右表中满足连接条件的记录信息
--right join(右连接):join右表中所有记录和左表中满足连接条件的记录信息
--5.查找所有员工的last_name和first_name以及对应部门编号dept_no
SELECT e.last_name,e.first_name,d.dept_no
FROM employees e
LEFT JOIN dept_emp d
ON d.emp_no
= e.emp_no;
--6.查找所有员工入职时候的薪水情况,给出emp_no以及salary, 并按照emp_no进行逆序
SELECT e.emp_no,s.salary
FROM employees e
INNER JOIN salaries s
ON s.emp_no
= e.emp_no
AND e.hire_date
= s.from_date
--少考虑这一种情况
ORDER BY e.emp_no
DESC;
--7.查找薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数t
-- 本题主要考聚合查询
SELECT emp_no,
COUNT(emp_no)
AS t
FROM salaries
GROUP BY emp_no
HAVING t
>15;
--8.找出所有员工当前(to_date=‘9999-01-01‘)具体的薪水salary情况,对于相同的薪水只显示一次,并按照逆序显示
SELECT DISTINCT(salary)
FROM salaries
WHERE to_date
=‘9999-01-01‘
ORDER BY salary
DESC;
--9.获取所有部门当前manager的当前薪水情况,给出dept_no, emp_no以及salary,当前表示to_date=‘9999-01-01‘
-- PS: on已经是条件的判断了,不能再用where, INNER JOIN 左右两表交换 没有 错,oj有问题
SELECT d.dept_no,d.emp_no,s.salary
FROM dept_manager d
INNER JOIN salaries s
ON s.emp_no
= d.emp_no
AND d.to_date
= ‘9999-01-01‘
AND s.to_date
= ‘9999-01-01‘
---WHERE to_date=‘9999-01-01‘;
---注:有人反映将连接语句改成FROM dept_manager AS d INNER JOIN salaries AS s后,结果通不过。
--INNER JOIN对于左右两表并无顺序要求,此为本题OJ系统Bug所致。
--10.获取所有非manager的员工emp_no !!!!!!
-- -PS:考察NOT IN
--方法一:使用NOT IN选出在employees但不在dept_manager中的emp_no记录
SELECT emp_no
FROM employees
WHERE emp_no
NOT IN (
SELECT emp_no
FROM dept_manager)
-- 方法二:先使用LEFT JOIN连接两张表,再从此表中选出dept_no值为NULL对应的emp_no记录
SELECT emp_no?
FROM?(
SELECT * FROM employees
LEFT JOIN dept_manager
ON employees.emp_no
= dept_manager.emp_no)
WHERE dept_no
IS NULL
--11.获取所有员工当前的manager,如果当前的manager是自己的话结果不显示,当前表示to_date=‘9999-01-01‘。
--结果第一列给出当前员工的emp_no,第二列给出其manager对应的manager_no。
SELECT de.emp_no, dm.emp_no
AS manager_no?
FROM dept_emp
AS de
INNER JOIN dept_manager
AS dm
ON de.dept_no
= dm.dept_no?
WHERE dm.to_date
= ‘9999-01-01‘ AND de.emp_no
<> dm.emp_no
--12.获取所有部门中当前员工薪水最高的相关信息,给出dept_no, emp_no以及其对应的salary
SELECT de.dept_no,de.emp_no,
MAX(sa.salary)
AS salary
FROM dept_emp
AS de
INNER JOIN salaries
AS sa
ON de.emp_no
= sa.emp_no
WHERE de.to_date
= ‘9999-01-01‘ AND sa.to_date
= ‘9999-01-01‘
GROUP BY de.dept_no
--13.从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。
--PS:正确的过滤是在having以后过滤
SELECT title,
COUNT(title)
AS t
FROM titles
WHERE t
>=2 --错误
GROUP BY title
-- 正确的答案
SELECT title,
COUNT(title)
AS t
FROM titles
GROUP BY title
HAVING t
>= 2;
--- 聚合就聚合到这里了
--14.从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。注意对于重复的emp_no进行忽略。
--PS:对COUNT中进行分解
SELECT title,
COUNT(
DISTINCT emp_no)
AS t
FROM titles
GROUP BY title
HAVING t
>= 2
--15.查找employees表所有emp_no为奇数,且last_name不为Mary的员工信息,并按照hire_date逆序排列
SELECT *
FROM employees
WHERE emp_no
%2 = 1 AND last_name
!=‘Mary‘
ORDER BY hire_date
DESC;
--16 .统计出当前各个title类型对应的员工当前薪水对应的平均工资。结果给出title以及平均工资avg。
--PS:不知道为什么不行
--SELECT t.title AVG(s.salary);
--FROM salaries s INNER JOIN titles t
--ON s.emp_no = t.emp_no AND t.to_date=‘9999-01-01‘ AND s.to_date=‘9999-01-01‘
--GROUP BY title
select t.title,
avg(s.salary)
from titles t, salaries s
where
s.emp_no = t.emp_no
and s.to_date
=?
‘9999-01-01‘
and t.to_date
=?
‘9999-01-01‘
group by title
--17.获取当前(to_date=‘9999-01-01‘)薪水第二多的员工的emp_no以及其对应的薪水salary
SELECT emp_no,salary
FROM salaries
WHERE to_date
=‘9999-01-01‘
ORDER BY salary
DESC --注意要降序
LIMIT
1,
1
--18.查找当前薪水(to_date=‘9999-01-01‘)排名第二多的员工编号emp_no、薪水salary、last_name以及first_name,不准使用order by
SELECT e.emp_no,
MAX(s.salary)
AS salary, e.last_name, e.first_name
FROM employees
AS e
INNER JOIN salaries
AS s
ON e.emp_no
= s.emp_no
AND s.to_date
= ‘9999-01-01‘
AND s.salary
NOT IN (
SELECT MAX(salary)
FROM salaries
WHERE salaries.to_date
= ‘9999-01-01‘);
--19.查找所有员工的last_name和first_name以及对应的dept_name,也包括暂时没有分配部门的员工
--毁三观!!!!!!
--1、第一次LEFT
--JOIN连接employees表与dept_emp表,得到所有员工的last_name和first_name以及对应的dept_no,也包括暂时没有分配部门的员工
--2、第二次LEFT
--JOIN连接上表与departments表,即连接dept_no与dept_name,得到所有员工的last_name和first_name以及对应的dept_name,也包括暂时没有分配部门的员工
select e.last_name,e.first_name,d.dept_name
from employees e
left join dept_emp de
on e.emp_no
=de.emp_no
left join departments d
on d.dept_no
=de.dept_no;
--20.查找员工编号emp_now为10001其自入职以来的薪水salary涨幅值growth
SELECT (
MAX(salary)
-MIN(salary))
AS growth
FROM salaries
WHERE emp_no
= ‘10001‘
--16 .统计出当前各个title类型对应的员工当前薪水对应的平均工资。结果给出title以及平均工资avg。
--PS:不知道为什么不行
--SELECT t.title AVG(s.salary);
--FROM salaries s INNER JOIN titles t
--ON s.emp_no = t.emp_no AND t.to_date=‘9999-01-01‘ AND s.to_date=‘9999-01-01‘
--GROUP BY title
select t.title,
avg(s.salary)
from titles t, salaries s
where
s.emp_no = t.emp_no
and s.to_date
=?
‘9999-01-01‘
and t.to_date
=?
‘9999-01-01‘
group by title
--17.获取当前(to_date=‘9999-01-01‘)薪水第二多的员工的emp_no以及其对应的薪水salary
SELECT emp_no,salary
FROM salaries
WHERE to_date
=‘9999-01-01‘
ORDER BY salary
DESC --注意要降序
LIMIT
1,
1
--18.查找当前薪水(to_date=‘9999-01-01‘)排名第二多的员工编号emp_no、薪水salary、last_name以及first_name,不准使用order by
SELECT e.emp_no,
MAX(s.salary)
AS salary, e.last_name, e.first_name
FROM employees
AS e
INNER JOIN salaries
AS s
ON e.emp_no
= s.emp_no
AND s.to_date
= ‘9999-01-01‘
AND s.salary
NOT IN (
SELECT MAX(salary)
FROM salaries
WHERE salaries.to_date
= ‘9999-01-01‘);
--19.查找所有员工的last_name和first_name以及对应的dept_name,也包括暂时没有分配部门的员工
--毁三观!!!!!!
--1、第一次LEFT
--JOIN连接employees表与dept_emp表,得到所有员工的last_name和first_name以及对应的dept_no,也包括暂时没有分配部门的员工
--2、第二次LEFT
--JOIN连接上表与departments表,即连接dept_no与dept_name,得到所有员工的last_name和first_name以及对应的dept_name,也包括暂时没有分配部门的员工
select e.last_name,e.first_name,d.dept_name
from employees e
left join dept_emp de
on e.emp_no
=de.emp_no
left join departments d
on d.dept_no
=de.dept_no;
--20.查找员工编号emp_now为10001其自入职以来的薪水salary涨幅值growth
SELECT (
MAX(salary)
-MIN(salary))
AS growth
FROM salaries
WHERE emp_no
= ‘10001‘
--21.查找所有员工自入职以来的薪水涨幅情况,给出员工编号emp_noy以及其对应的薪水涨幅growth,并按照growth进行升序
select t1.emp_no, t1.salary
- t2.salary
as growth
from
(select e.emp_no,s.salary
from salaries s,employees e
where e.emp_no
=s.emp_no
and s.to_date
=‘9999-01-01‘ )
as t1,
(select e.emp_no,s.salary
from salaries s,employees e
where e.emp_no
=s.emp_no
and s.from_date
=e.hire_date)
as t2
where t1.emp_no
=t2.emp_no
order by growth;
--22.统计各个部门对应员工涨幅的次数总和,给出部门编码dept_no、部门名称dept_name以及次数sum
-- 多个表查询,可以多次链接
SELECT de.dept_no, dp.dept_name,
COUNT(s.salary)
AS sum?
FROM (dept_emp
AS de
INNER JOIN salaries
AS s
ON de.emp_no
= s.emp_no)?
INNER JOIN departments
AS dp
ON de.dept_no
= dp.dept_no?
GROUP BY de.dept_no
--23.对所有员工的当前(to_date=‘9999-01-01‘)薪水按照salary进行按照1-N的排名,相同salary并列且按照emp_no升序排列
SELECT emp_no,salary,
AS rank
FROM salaries
WHERE to_date
=‘9999-01-01‘
ORDER BY emp_no
--24.对所有员工的当前(to_date=‘9999-01-01‘)薪水按照salary进行按照1-N的排名,相同salary并列且按照emp_no升序排列
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
--最大的数只小于等于自己,第二大的数只小于等于两个数,以此类推。。。为他们的rank
group by s1.emp_no
order by rank ;
--25.获取所有非manager员工当前的薪水情况,给出dept_no、emp_no以及salary ,当前表示to_date=‘9999-01-01‘
SELECT de.dept_no, s.emp_no, s.salary?
FROM (employees
AS e
INNER JOIN salaries
AS s
ON s.emp_no
= e.emp_no
AND s.to_date
= ‘9999-01-01‘)
INNER JOIN dept_emp
AS de
ON e.emp_no
= de.emp_no
WHERE de.emp_no
NOT IN (
SELECT emp_no
FROM dept_manager)
--25.获取员工其当前的薪水比其manager当前薪水还高的相关信息,当前表示to_date=‘9999-01-01‘,结果第一列给出员工的emp_no,第二列给出其manager的manager_no,第三列给出该员工当前的薪水emp_salary,第四列给该员工对应的manager当前的薪水manager_salary
--SELECT
--FROM dept_emp e INNER JOIN dept_manager m
--ON e.emp_no = m.emp_no
--本题主要思想是创建两张表(一张记录当前所有员工的工资,另一张只记录部门经理的工资)进行比较,具体思路如下:
-- 1、先用INNER JOIN连接salaries和demp_emp,建立当前所有员工的工资记录sem
-- 2、再用INNER JOIN连接salaries和demp_manager,建立当前所有员工的工资记录sdm
-- 3、最后用限制条件sem.dept_no = sdm.dept_no AND sem.salary >
-- sdm.salary找出同一部门中工资比经理高的员工,并根据题意依次输出emp_no、manager_no、emp_salary、manager_salary
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
--26.汇总各个部门当前员工的title类型的分配数目,结果给出部门编号dept_no、dept_name、其当前员工所有的title以及该类型title对应的数目count
--PS:对一个表进行连接以后,可以再连接两一个表
本题的关键在于用 GROUP BY 同时对 de.dept_no 和 t.title
进行分组,具体思路如下:
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
--27.给出每个员工每年薪水涨幅超过5000的员工编号emp_no、薪水变更开始日期from_date以及薪水涨幅值salary_growth,并按照salary_growth逆序排列。
-- 提示:在sqlite中获取datetime时间对应的年份函数为strftime(‘%Y‘, to_date)
--SELECT emp_no,from_date, AS salary_growth
--FROM salaries
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
--28.查找描述信息中包括robot的电影对应的分类名称以及电影数目,而且还需要该分类对应电影数量>=5部
SELECT c.name AS name, COUNT(c.name) AS amount
FROM (film AS f INNER JOIN film_category AS fc ON f.film_id = fc.film_id )
INNER JOIN category AS c
ON fc.category_id = c.category_id
WHERE f.description LIKE ‘%robot%‘
GROUP BY c.name HAVING amount >= 2
--29.使用join查询方式找出没有分类的电影id以及名称
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
--30.使用子查询的方式找出属于Action分类的所有电影对应的title,description
SELECT f.title, f.description
FROM film f, film_category fc, category c
WHERE f.film_id = fc.film_id
AND fc.category_id = c.category_id
AND c.name = ‘Action‘
--32.将employees表的所有员工的last_name和first_name拼接起来作为Name,中间以一个空格区分
--不同数据库连接字符串的方法不完全相同,MySQL、SQL
--Server、Oracle等数据库支持CONCAT方法,而本题所用的SQLite数据库只支持用连接符号"||"来连接字符串
SELECT last_name||" "||first_name AS Name FROM employees
--33.创建一个actor表,包含如下列信息
CREATE TABLE IF NOT EXISTS actor
(
actor_id smallint(5) NOT NULL PRIMARY KEY,
first_name varchar(45) NOT NULL,
last_name varchar(45) NOT NULL,
last_update timestamp NOT NULL DEFAULT (datetime(‘now‘,‘localtime‘))
)
--34.批量插入数据
INSERT INTO actor
VALUES (1, ‘PENELOPE‘, ‘GUINESS‘, ‘2006-02-15 12:34:33‘),
(2, ‘NICK‘, ‘WAHLBERG‘, ‘2006-02-15 12:34:33‘)
--35.批量插入数据
INSERT OR IGNORE INTO actor VALUES (3, ‘ED‘, ‘CHASE‘, ‘2006-02-15 12:34:33‘)
--36.创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。 actor_name表结构如下:
CREATE TABLE actor_name
(
first_name varchar(45) NOT NULL,
last_name varchar(45) NOT NULL
);
INSERT INTO actor_name SELECT first_name, last_name FROM actor;
--37.对first_name创建唯一索引uniq_idx_firstname,对last_name创建普通索引idx_lastname
CREATE UNIQUE INDEX uniq_idx_firstname ON actor(first_name);
CREATE INDEX idx_lastname ON actor(last_name);
--38.针对actor表创建视图actor_name_view,只包含first_name以及last_name两列,并对这两列重新命名,fist_name为first_name_v,last_name修改为last_name_v:
CREATE TABLE IF NOT EXISTS actor (
actor_id smallint(5) NOT NULL PRIMARY KEY,
first_name varchar(45) NOT NULL,
last_name varchar(45) NOT NULL,
last_update timestamp NOT NULL DEFAULT (datetime(‘now‘,‘localtime‘)))
-------------------------------
--视图是用来干什么的?视图时虚表,仅存储建立视图的sql语句,查询的时候跟普通的表一样,
CREATE VIEW actor_name_view AS
SELECT first_name AS fist_name_v, last_name AS last_name_v
FROM actor
--39.针对salaries表emp_no字段创建索引idx_emp_no,查询emp_no为10005, 使用强制索引。
SELECT * FROM salaries INDEXED BY idx_emp_no WHERE emp_no=‘10005‘;
--40.现在在last_update后面新增加一列名字为create_date, 类型为datetime, NOT NULL,默认值为‘0000 00:00:00‘
--ADD COLUMN create_date
ALTER TABLE actor ADD COLUMN create_date datetime NOT NULL DEFAULT ‘0000-00-00 00:00:00‘;
--41.构造一个触发器audit_log,在向employees表中插入一条数据的时候,触发插入相关的数据到audit中。
CREATE TRIGGER audit_log AFTER INSERT ON employees_test
BEGIN
INSERT INTO audit VALUES (NEW.ID, NEW.NAME);
END;
--42.删除emp_no重复的记录,只保留最小的id对应的记录。
DELETE FROM titles_test WHERE id NOT IN
(SELECT MIN(id) FROM titles_test GROUP BY emp_no);
--43.将所有to_date为9999-01-01的全部更新为NULL,且 from_date更新为2001-01-01。
UPDATE FROM titles_test SET to_date =NULL, from_date=‘2001-01-01‘
WHERE to_date=‘9999-01-01‘;
--44.将id=5以及emp_no=10001的行数据替换成id=5以及emp_no=10005,其他数据保持不变,使用replace实现。
update titles_test set emp_no=replace(emp_no,‘10001‘,‘10005‘) where id=‘5‘
--45.将titles_test表名修改为titles_2017。
-- RENAME TABLE titles_test AS titles_2017;
ALTER TABLE titles_test RENAME TO