1、查找最晚入职员工的所有信息
SELECT * FROM employees
WHERE hire_date = (
SELECT MAX(hire_date) FROM employees)
limit 0,1 0后面选1个 就是第一名
2、查找入职员工时间排名倒数第三的员工所有信息
select * from employees
order by hire_date desc limit 2,1 2 后面选1个 就是第三名
3、查找各个部门当前(to_date=‘9999-01-01’)领导当前薪水详情以及其对应部门编号dept_no
select s.*,d.dept_no
from salaries s
inner join dept_manager d on d.emp_no = s.emp_no
and d.to_date = ‘9999-01-01’
and s.to_date = ‘9999-01-01’;
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 e.emp_no = d.emp_no
join的用法还是不太会 inner 还是 left 只能是当下的最大提升 siki那个是无用功 就刷题 做项目是最快的 视频就看
6、查找所有员工入职时候的薪水情况,给出emp_no以及salary, 并按照emp_no进行逆序
select e.emp_no,s.salary
from employees e
inner join salaries s on e.emp_no = s.emp_no
and e.hire_date = s.from_date
order by e.emp_no desc
select s.emp_no,s.salary from employees e,salaries s
where s.emp_no = e.emp_no and e.hire_date = s.from_date
order by s.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
select a.* from(。。。) as a
where t>15
。。。
select emp_no,count(*) as t
from salaries
group by emp_no
10、获取所有非manager的员工emp_no
select e.emp_no
from employees e
left join dept_manager d on e.emp_no = d.emp_no
where d.emp_no is null;
select emp_no from employees
where emp_no not in
(select emp_no from dept_manager)
11、获取所有员工当前的manager,如果当前的manager是自己的话结果不显示,当前表示to_date=‘9999-01-01’。
SELECT de.emp_no, dm.emp_no AS manager_no
FROM dept_emp AS de
LEFT 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
select e.emp_no,m.emp_no as manager_no
from dept_emp e,dept_manager m
where e.dept_no=m.dept_no
and m.to_date=‘9999-01-01’
and e.emp_no<>m.emp_no
12、获取所有部门中当前员工薪水最高的相关信息
select d.dept_no,d.emp_no,max(s.salary) as salary
from dept_emp d
left join salaries s
on s.emp_no=d.emp_no and d.to_date = s.to_date
where d.to_date = ‘9999-01-01’
group by d.dept_no
SELECT a.dept_no,a.emp_no,max(b.salary) salary
FROM dept_emp a,salaries b
WHERE a.to_date=‘9999-01-01’
AND b.to_date=‘9999-01-01’
AND a.emp_no=b.emp_no
GROUP BY a.dept_no
select b.dept_no, a.emp_no, max(a.salary) as salary
from salaries a
inner join dept_emp b
on a.emp_no = b.emp_no
where a.to_date = ‘9999-01-01’ and b.to_date = ‘9999-01-01’
group by b.dept_no
13、从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t
CREATE TABLE titles
(
emp_no
int(11) NOT NULL,
title
varchar(50) NOT NULL,
from_date
date NOT NULL,
to_date
date DEFAULT NULL);
SELECT title,COUNT(title) AS t
FROM titles
GROUP BY title
having t>=2;
14、注意对于重复的emp_no进行忽略
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逆序排列
last_name <> ‘Mary’
where emp_no%2 =1 奇数%
16、统计出当前各个title类型对应的员工当前(to_date=‘9999-01-01’)薪水对应的平均工资。结果给出title以及平均工资avg
select t.title,avg(s.salary) as avg
from salaries s
innerjoin titles t on s.emp_no=t.emp_no
where t.to_date=‘9999-01-01’ and s.to_date=‘9999-01-01’
group by t.title
select t.title , avg(salary) avg
from salaries s,titles t
where t.emp_no = s.emp_no
and t.to_date =‘9999-01-01’
and s.to_date =‘9999-01-01’
group by t.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(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 s.to_date=‘9999-01-01’
and s.salary not in (select max(salary) from salaries )
select e.emp_no,max(salary) ,e.last_name,e.first_name
from salaries as s,employees as e
where s.emp_no = e.emp_no
and salary<
(
select max(salary)
from salaries
where to_date = ‘9999-01-01’
);
19、查找所有员工的last_name和first_name以及对应的dept_name,也包括暂时没有分配部门的员工
错解 select e.last_name,e.first_name,d.dept_name
from dept_emp de
left join employees e on de.emp_no=e.emp_no
left join departments on de.dept_no=d.dept_no
正解 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;
select e.last_name, e.first_name, d.dept_name
from (employees AS e left join dept_emp AS de on e.emp_no=de.emp_no)
left join departments AS d on de.dept_no=d.dept_no
select e.last_name,e.first_name,d.dept_name
from (employees e left join dept_emp s on s.emp_no=e.emp_no)
left join departments d on s.dept_no=d.dept_no;
20、查找员工编号emp_no为10001其自入职以来的薪水salary涨幅值growth
select (max(salary)-min(salary)) as growth
from salaries
where emp_no =‘10001’
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;
21、查找所有员工自入职以来的薪水涨幅情况,给出员工编号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
SELECT send.emp_no, (send.salary-sstart.salary) AS growth
FROM
(SELECT e.emp_no,s.salary
FROM employees AS e
JOIN salaries AS s
ON e.emp_no=s.emp_no
WHERE s.to_date=‘9999-01-01’) AS send
JOIN
(SELECT s.emp_no,s.salary
FROM employees AS e
JOIN salaries AS s
ON e.emp_no=s.emp_no
WHERE e.hire_date=s.from_date) AS sstart
ON sstart.emp_no=send.emp_no
ORDER BY growth ASC
22、统计各个部门的工资记录数,给出部门编码dept_no、部门名称dept_name以及次数sum
select a.dept_no, a.dept_name, count(c.emp_no) as sum
from departments as a
inner join dept_emp as b on a.dept_no = b.dept_no
inner join salaries as c on b.emp_no = c.emp_no
group by b.dept_no
SELECT a.dept_no, b.dept_name, COUNT(s.salary) AS sum
FROM (dept_emp AS a INNER JOIN salaries AS s ON a.emp_no = s.emp_no)
INNER JOIN departments AS b ON a.dept_no = b.dept_no
GROUP BY a.dept_no
select b.dept_no ,c.dept_name,count(salary) as sum
from salaries a ,dept_emp b,departments c
where a.emp_no = b.emp_no and b.dept_no=c.dept_no
group by c.dept_no
23、对所有员工的当前(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.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;
24、获取所有非manager员工当前的薪水情况,给出dept_no、emp_no以及salary ,当前表示to_date=‘9999-01-01’
select a.dept_no,s.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 a on e.emp_no=a.emp_no where a.emp_no not in
(select emp_no from dept_manager WHERE to_date = ‘9999-01-01’)
SELECT a.dept_no,a.emp_no,s.salary
FROM dept_emp a,salaries s
WHERE a.emp_no=s.emp_no
AND a.to_date=‘9999-01-01’
AND s.to_date=‘9999-01-01’
AND a.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 s1.emp_no, s2.emp_no AS manager_no, s1.salary AS emp_salary, s2.salary AS manager_salary
FROM
(SELECT s.emp_no, a.dept_no, s.salary FROM salaries s INNER JOIN dept_emp a ON s.emp_no = a.emp_no AND s.to_date = ‘9999-01-01’)
AS s1,
(SELECT s.emp_no, b.dept_no, s.salary FROM salaries s INNER JOIN dept_manager b ON s.emp_no = b.emp_no AND s.to_date = ‘9999-01-01’)
AS s2
WHERE s1.dept_no = s2.dept_no
AND s1.salary > s2.salary
select dept_emp.emp_no, dept_manager.emp_no as manager_no, s1.salary as emp_salary, s2.salary as manager_salary
from dept_emp, dept_manager, salaries as s1, salaries as s2
where dept_emp.to_date=‘9999-01-01’
and dept_manager.to_date=‘9999-01-01’
and s2.to_date=‘9999-01-01’
and s1.to_date=‘9999-01-01’
and s1.salary > s2.salary
and dept_emp.emp_no=s1.emp_no
and dept_manager.emp_no=s2.emp_no
and dept_manager.dept_no=dept_emp.dept_no
26、汇总各个部门当前员工的title类型的分配数目,结果给出部门编号dept_no、dept_name、
其当前员工所有的title以及该类型title对应的数目count
SELECT a.dept_no, b.dept_name, t.title, COUNT(t.title) AS count
FROM titles AS t
INNER JOIN dept_emp AS a ON t.emp_no = a.emp_no AND a.to_date = ‘9999-01-01’ AND t.to_date = ‘9999-01-01’
INNER JOIN departments AS b ON a.dept_no = b.dept_no
GROUP BY a.dept_no, t.title
27、给出每个员工每年薪水涨幅超过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;
28、查找描述信息中包括robot的电影对应的分类名称以及电影数目,而且还需要该分类对应电影数量>=5部
select c.name, count(f.film_id) as t
from film f, category c, film_category fc
where f.description like ‘%robot%’
and f.film_id = fc.film_id
and c.category_id = fc.category_id
group by c.category_id
having t>=2
select c.name,count(f.title)
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.category_id
having count(f.title)>=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
select film_id,title
from film
where film_id not in (select film_id from film_category)
30、使用子查询的方式找出属于Action分类的所有电影对应的title,description
select title,description
from film
where film_id in
(
select film_id from film_category fc
inner join category c on fc.category_id=c.category_id 开心 自己写出来的
where c.name like ‘%action%’
)
31、获取select * from employees对应的执行计划
explain select * from employees;
32、将employees表的所有员工的last_name和first_name拼接起来作为Name,中间以一个空格区分
SELECT last_name||" "||first_name AS Name FROM employees
33、创建一个actor表
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’))
)
34、对于表actor批量插入如下数据
insert into actor values
(1,‘PENELOPE’,‘GUINESS’,‘2006-02-15 12:34:33’),
(2,‘NICK’,‘WAHLBERG’,‘2006-02-15 12:34:33’);
35、对于表actor批量插入如下数据,如果数据已经存在,请忽略
insert or ignore into actor values
(3,‘ED’,‘CHASE’,‘2006-02-15 12:34:33’);
35、创建一个actor_name表,将actor表中的所有first_name以及last_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;
create table actor_name as
select first_name,last_name
from actor
36、对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 view actor_name_view (first_name_v,last_name_v) as
select first_name,last_name from actor
create view actor_name_view as
select first_name first_name_v,
last_name last_name_v
from actor
38、对salaries表emp_no字段创建索引idx_emp_no,查询emp_no为10005, 使用强制索引。
select * from salaries
indexed by idx_emp_no
where emp_no = ‘10005’
39、在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 trigger audit_log after insert on employees_test
begin
insert into audit values(NEW.id,NEW.name);
end;
41、删除emp_no重复的记录,只保留最小的id对应的记录
delete from titles_test where id not in(SELECT MIN(id) FROM titles_test GROUP BY emp_no)
42、将所有to_date为9999-01-01的全部更新为NULL,且 from_date更新为2001-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实现
replace into titles_test
values (5,‘10005’,‘Senior Engineer’, ‘1986-06-26’, ‘9999-01-01’)
44、将titles_test表名修改为titles_2017
alter table titles_test rename to titles_2017
45、在audit表上创建外键约束,其emp_no对应employees_test表的主键id
drop table audit;
create table audit(
EMP_no INT NOT NULL,
create_date datetime NOT NULL,
foreign key(EMP_no) references employees_test(ID))
46、存在如下的视图:create view emp_v as select * from employees where emp_no >10005;
如何获取emp_v和employees有相同的数据?
select * from employees where emp_no >10005;
47、将所有获取奖金的员工当前的薪水增加10%
update salaries
set salary = salary * 1.1
48、针对库中的所有表生成select count()对应的SQL语句
SELECT "select count() from " || name || “;” AS cnts
FROM sqlite_master WHERE type = ‘table’
49、将employees表中的所有员工的last_name和first_name通过(’)连接起来
SELECT last_name || “’” || first_name FROM employees
50、查找字符串’10,A,B’ 中逗号’,‘出现的次数cnt
select (length(‘10,A,B’)-length(replace(‘10,A,B’,’,’,’’)))as cnt;
select (length(“10,A,B”)-length(replace(“10,A,B”,",",""))) as cnt;
51、获取Employees中的first_name,查询按照first_name最后两个字母,按照升序进行排列
SELECT first_name FROM employees
order by substr(first_name,length(first_name)-1)
select first_name from employees
order by substr(first_name,-2);
52、按照dept_no进行汇总,属于同一个部门的emp_no按照逗号进行连接,结果给出dept_no以及连接出的结果employees
SELECT dept_no, group_concat(emp_no) AS employees
FROM dept_emp
GROUP BY dept_no;
53、查找排除当前最大、最小salary之后的员工的平均工资avg_salary
select avg(salary) as avg_salary from salaries
where salary<>(select max(salary) as max_s from salaries)
and salary <>(select min(salary) as min_s from salaries)
and to_date=‘9999-01-01’
54、分页查询employees表,每5行一页,返回第2页的数据
select * from employees limit 5,5
55、获取所有员工的emp_no、部门编号dept_no以及对应的bonus类型btype和received ,没有分配具体的员工不显示
SELECT a.emp_no, a.dept_no, b.btype, b.recevied
FROM dept_emp AS a
LEFT OUTER JOIN emp_bonus AS b
ON a.emp_no = b.emp_no;
56、使用含有关键字exists查找未分配具体部门的员工的所有信息
select * from employees
where not exists
(select emp_no from dept_emp where employees.emp_no = emp_no)
select * from employees
where emp_no not in (select emp_no from dept_emp)
57、获取employees中的行数据,且这些行也存在于emp_v中。注意不能使用intersect关键字
select * from employees where emp_no >10005;
select * from emp_v
58、bonus类型btype为1其奖金为薪水salary的10%,btype为2其奖金为薪水的20%,其他类型均为薪水的30%。
select e.emp_no,e.first_name,e.last_name,b.btype,s.salary,
(case b.btype
when 1 then s.salary * 0.1
when 2 then s.salary * 0.2
else s.salary * 0.3 end) as bonus
from employees e
inner join emp_bonus b on e.emp_no=b.emp_no
inner join salaries s on s.emp_no=e.emp_no and s.to_date=‘9999-01-01’;
59、按照salary的累计和running_total,其中running_total为前两个员工的salary累计和,其他以此类推
select a.emp_no,a.salary,
(select sum(s.salary) from salaries as s where s.emp_no <=a.emp_no and s.to_date=‘9999-01-01’)as running_total
from salaries as a
where a.to_date=‘9999-01-01’
order by a.emp_no;
60、对于employees表中,给出奇数行的first_name
select e1.first_name
from employees as e1
where(select count(*) from employees as e2
where e1.first_name>=e2.first_name)%2=1;