select * from employees
where emp_no % 2 = 1
and last_name !=
‘Mary‘
order by hire_date desc
15.统计出当前各个title类型对应的员工当前薪水对应的平均工资。结果给出title以及平均工资avg
select title,avg(salary) as avg
from titles a
inner join salaries b
on a.emp_no=b.emp_no and a.to_date =
‘9999-01-01‘
AND b.to_date =
‘9999-01-01‘
group by a.title
16.获取当前(to_date=‘9999-01-01‘)薪水第二多的员工的emp_no以及其对应的薪水salary
select emp_no,salary from salaries s where s.to_date =
‘9999-01-01‘
order by s.salary desc limit 1,1
17.查找当前薪水(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
where to_date=
‘9999-01-01‘
and salary not in
(select max (salary) from salaries where to_date=
‘9999-01-01‘
)
18.查找所有员工的last_name和first_name以及对应的dept_name,也包括暂时没有分配部门的员工SELECT
e.last_name, e.first_name, dp.dept_name FROM employees AS e
LEFT JOIN dept_emp AS d ON e.emp_no = d.emp_no
LEFT JOIN departments AS dp ON d.dept_no = dp.dept_no
19.查找员工编号emp_no为10001其自入职以来的薪水salary涨幅值growth
SELECT (
(SELECT salary FROM salaries WHERE emp_no = 10001 ORDER BY to_date DESC LIMIT 1)
- (SELECT salary FROM salaries WHERE emp_no = 10001 ORDER BY to_date LIMIT 1)
) AS growth;
20.查找所有员工自入职以来的薪水涨幅情况,给出员工编号emp_no以及其对应的薪水涨幅growth,并按照growth进行升序
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
21.统计各个部门对应员工涨幅的次数总和,给出部门编码dept_no、部门名称dept_name以及次数sum
select d.dept_no ,dept.dept_name,count(salary) from salaries s ,dept_emp d,departments dept
where s.emp_no = d.emp_no and d.dept_no=dept.dept_no group by dept.dept_no
22.对所有员工的当前(to_date=‘9999-01-01‘)薪水按照salary进行按照1-N的排名,相同salary并列且按照emp_no升序排列
select s1.emp_no,s1.salary,count(distinct s2.salary) rank
from salaries s1, salaries s2
where s1.salary <= s2.salary and s1.to_date =
‘9999-01-01‘
and s2.to_date =
‘9999-01-01‘
group by s1.emp_no order by rank;
23.获取所有非manager员工当前的薪水情况,给出dept_no、emp_no以及salary ,当前表示to_date=‘9999-01-01‘
SELECT de.dept_no,de.emp_no,sa.salary
FROM dept_emp de,salaries sa
WHERE de.emp_no=sa.emp_no AND de.to_date=
‘9999-01-01‘
AND sa.to_date=
‘9999-01-01‘
AND de.emp_no NOT IN(
SELECT emp_no
FROM dept_manager
)
24.获取员工其当前的薪水比其manager当前薪水还高的相关信息,当前表示to_date=‘9999-01-01‘,
结果第一列给出员工的emp_no,
第二列给出其manager的manager_no,
第三列给出该员工当前的薪水emp_salary,
第四列给该员工对应的manager当前的薪水manager_salary
SELECT s1.emp_no AS emp_no, s2.emp_no AS manager_no, s1.salary AS emp_salary, s2.salary AS manager_salary
FROM
(SELECT s.emp_no, de.dept_no, s.salary FROM salaries s INNER JOIN dept_emp de ON s.emp_no = de.emp_no AND s.to_date =
‘9999-01-01‘
)
AS s1,
(SELECT s.emp_no, dm.dept_no, s.salary FROM salaries s INNER JOIN dept_manager dm ON s.emp_no = dm.emp_no AND s.to_date =
‘9999-01-01‘
)
AS s2
WHERE s1.dept_no = s2.dept_no
AND s1.salary > s2.salary
25.汇总各个部门当前员工的title类型的分配数目,结果给出部门编号dept_no、dept_name、其当前员工所有的title以及该类型title对应的数目count
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
26.给出每个员工每年薪水涨幅超过5000的员工编号emp_no、薪水变更开始日期from_date以及薪水涨幅值salary_growth,并按照salary_growth逆序排列。
提示:在sqlite中获取datetime时间对应的年份函数为strftime(‘%Y‘, to_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 s2.salary - s1.salary > 5000
AND
strftime
(
‘%Y‘
,s2.to_date) -
strftime
(
‘%Y‘
,s1.to_date) = 1
ORDER BY salary_growth DESC;
27.查找描述信息中包括robot的电影对应的分类名称以及电影数目,而且还需要该分类对应电影数量>=5部
select c.name,count(fcc.film_id)as num
from film_category fc
inner join category c on c.category_id=fc.category_id
inner join (select * from film f where f.description like
‘%robot%‘
) as f on f.film_id=fc.film_id
inner join (select *,count (fc.film_id)as num from film_category fc group by category_id having num>=5)as fcc on fc.category_id=fcc.category_id
28.使用join查询方式找出没有分类的电影id以及名称
select f.film_id, f.title
from film as f
left join film_category as fc
on f.film_id = fc.film_id
where fc.category_id is null
29.使用子查询的方式找出属于Action分类的所有电影对应的title,description
select title, description
from film
where film_id in (select film_id from film_category where category_id = (select category_id from category where name =
‘Action‘
));
30.获取select * from employees对应的执行计划
explain select * from employees
31.将employees表的所有员工的last_name和first_name拼接起来作为Name,中间以一个空格区分
SELECT last_name||
" "
||first_name AS Name FROM employees
32.创建一个actor表,包含如下列信息
列表 | 类型 | 是否为NULL | 含义 |
actor_id |
smallint(5) |
not null |
主键id |
first_name |
varchar(45) |
not null |
名字 |
last_name |
varchar(45) |
not null |
姓氏 |
last_update |
timestamp |
not null |
最后更新时间,默认是系统的当前时间 |
CREATE TABLE 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‘
)) -- ,
-- PRIMARY KEY(actor_id)
)
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‘)))
actor_id | first_name | last_name | last_update |
1 |
PENELOPE |
GUINESS |
2006-02-15 12:34:33 |
2 |
NICK |
WAHLBERG |
2006-02-15 12:34:33 |
INSERT INTO actor
values (1,
‘PENELOPE‘
,
‘GUINESS‘
,
‘2006-02-15 12:34:33‘
),(2,
‘NICK‘
,
‘WAHLBERG‘
,
‘2006-02-15 12:34:33‘
)
34.对于表actor批量插入如下数据,如果数据已经存在,请忽略,不使用replace操作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‘)))
actor_id | first_name | last_name | last_update |
‘3‘ |
‘ED‘ |
‘CHASE‘ |
‘2006-02-15 12:34:33‘ |
INSERT or IGNORE INTO actor
VALUES (
‘3‘
,
‘ED‘
,
‘CHASE‘
,
‘2006-02-15 12:34:33‘
);
35.对于如下表actor,其对应的数据为:
actor_id | first_name | last_name | last_update |
1 |
PENELOPE |
GUINESS |
2006-02-15 12:34:33 |
2 |
NICK |
WAHLBERG |
2006-02-15 12:34:33 |
创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。 actor_name表结构如下:
列表 | 类型 | 是否为NULL | 含义 |
first_name |
varchar(45) |
not null |
名字 |
last_name |
varchar(45) |
not null |
姓氏 |
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;
36.针对如下表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‘)))
对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);
37.针对actor表创建视图actor_name_view,只包含first_name以及last_name两列,并对这两列重新命名,first_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‘)))
create view actor_name_view (first_name_v,last_name_v) as
select first_name,last_name from actor
38.针对salaries表emp_no字段创建索引idx_emp_no,查询emp_no为10005, 使用强制索引。
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`));
create index idx_emp_no on salaries(emp_no);
select * from salaries
indexed by idx_emp_no
where emp_no =
‘10005‘
39.存在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‘)));
现在在last_update后面新增加一列名字为create_date, 类型为datetime, NOT NULL,默认值为‘0000 00:00:00‘
ALTER TABLE actor ADD COLUMN create_date datetime NOT NULL DEFAULT
‘0000-00-00 00:00:00‘
;
40.构造一个触发器audit_log,在向employees_test表中插入一条数据的时候,触发插入相关的数据到audit中。
CREATE TABLE employees_test(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL
);
CREATE TABLE audit(
EMP_no INT NOT NULL,
NAME TEXT NOT NULL
);
create trigger audit_log after insert on employees_test
begin
insert into audit values(
new
.id,
new
.name);
end;
41.删除emp_no重复的记录,只保留最小的id对应的记录。
CREATE TABLE IF NOT EXISTS titles_test (
id int(11) not null primary key,
emp_no int(11) NOT NULL,
title varchar(50) NOT NULL,
from_date date NOT NULL,
to_date date DEFAULT NULL);
insert into titles_test values (‘1‘, ‘10001‘, ‘Senior Engineer‘, ‘1986-06-26‘, ‘9999-01-01‘),
(‘2‘, ‘10002‘, ‘Staff‘, ‘1996-08-03‘, ‘9999-01-01‘),
(‘3‘, ‘10003‘, ‘Senior Engineer‘, ‘1995-12-03‘, ‘9999-01-01‘),
(‘4‘, ‘10004‘, ‘Senior Engineer‘, ‘1995-12-03‘, ‘9999-01-01‘),
(‘5‘, ‘10001‘, ‘Senior Engineer‘, ‘1986-06-26‘, ‘9999-01-01‘),
(‘6‘, ‘10002‘, ‘Staff‘, ‘1996-08-03‘, ‘9999-01-01‘),
(‘7‘, ‘10003‘, ‘Senior Engineer‘, ‘1995-12-03‘, ‘9999-01-01‘);
delete
from titles_test
where id not in (select min(id) from titles_test)
42.将所有to_date为9999-01-01的全部更新为NULL,且 from_date更新为2001-01-01。
CREATE TABLE IF NOT EXISTS titles_test (
id int(11) not null primary key,
emp_no int(11) NOT NULL,
title varchar(50) NOT NULL,
from_date date NOT NULL,
to_date date DEFAULT NULL);
insert into titles_test values (‘1‘, ‘10001‘, ‘Senior Engineer‘, ‘1986-06-26‘, ‘9999-01-01‘),
(‘2‘, ‘10002‘, ‘Staff‘, ‘1996-08-03‘, ‘9999-01-01‘),
(‘3‘, ‘10003‘, ‘Senior Engineer‘, ‘1995-12-03‘, ‘9999-01-01‘),
(‘4‘, ‘10004‘, ‘Senior Engineer‘, ‘1995-12-03‘, ‘9999-01-01‘),
(‘5‘, ‘10001‘, ‘Senior Engineer‘, ‘1986-06-26‘, ‘9999-01-01‘),
(‘6‘, ‘10002‘, ‘Staff‘, ‘1996-08-03‘, ‘9999-01-01‘),
(‘7‘, ‘10003‘, ‘Senior Engineer‘, ‘1995-12-03‘, ‘9999-01-01‘);
update titles_test set to_date=null,from_date=
‘2001-01-01‘
where to_date=
‘9999-01-01‘
;
43.将id=5以及emp_no=10001的行数据替换成id=5以及emp_no=10005,其他数据保持不变,使用replace实现。
CREATE TABLE IF NOT EXISTS titles_test (
id int(11) not null primary key,
emp_no int(11) NOT NULL,
title varchar(50) NOT NULL,
from_date date NOT NULL,
to_date date DEFAULT NULL);
insert into titles_test values (‘1‘, ‘10001‘, ‘Senior Engineer‘, ‘1986-06-26‘, ‘9999-01-01‘),
(‘2‘, ‘10002‘, ‘Staff‘, ‘1996-08-03‘, ‘9999-01-01‘),
(‘3‘, ‘10003‘, ‘Senior Engineer‘, ‘1995-12-03‘, ‘9999-01-01‘),
(‘4‘, ‘10004‘, ‘Senior Engineer‘, ‘1995-12-03‘, ‘9999-01-01‘),
(‘5‘, ‘10001‘, ‘Senior Engineer‘, ‘1986-06-26‘, ‘9999-01-01‘),
(‘6‘, ‘10002‘, ‘Staff‘, ‘1996-08-03‘, ‘9999-01-01‘),
(‘7‘, ‘10003‘, ‘Senior Engineer‘, ‘1995-12-03‘, ‘9999-01-01‘);
replace into titles_test values(5, 10005,
‘Senior Engineer‘
,
‘1986-06-26‘
,
‘9999-01-01‘
);
44.将titles_test表名修改为titles_2017。
CREATE TABLE IF NOT EXISTS titles_test (
id int(11) not null primary key,
emp_no int(11) NOT NULL,
title varchar(50) NOT NULL,
from_date date NOT NULL,
to_date date DEFAULT NULL);
insert into titles_test values (‘1‘, ‘10001‘, ‘Senior Engineer‘, ‘1986-06-26‘, ‘9999-01-01‘),
(‘2‘, ‘10002‘, ‘Staff‘, ‘1996-08-03‘, ‘9999-01-01‘),
(‘3‘, ‘10003‘, ‘Senior Engineer‘, ‘1995-12-03‘, ‘9999-01-01‘),
(‘4‘, ‘10004‘, ‘Senior Engineer‘, ‘1995-12-03‘, ‘9999-01-01‘),
(‘5‘, ‘10001‘, ‘Senior Engineer‘, ‘1986-06-26‘, ‘9999-01-01‘),
(‘6‘, ‘10002‘, ‘Staff‘, ‘1996-08-03‘, ‘9999-01-01‘),
(‘7‘, ‘10003‘, ‘Senior Engineer‘, ‘1995-12-03‘, ‘9999-01-01‘);
alter table titles_test
rename
to titles_2017