IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> SQL编程 -> 正文阅读

[大数据]SQL编程

#员工系列

#员工表
create table `employees` (
	`emp_no` int(11) not null,
    `birth_data` 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`));
#部门员工表
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,
    `to_date` date not null,
    primary key (`emp_no`,`dept_no`));
#职称表
create table `titles` (
	`emp_no` int(11) not null,
    `title` varchar(50) not null,
	`from_date` date not null,
	`to_date` date default null);

#1、查找入职最晚员工的所有信息
select * from employees
order by hire_date desc
limit 1
#2、查找入职员工时间排名倒数第三的员工所以信息
select * from employees
order by hire_date desc
limit 2,1
#3、查找各个部门当前领导当前薪水详情以及其对应部门的编号
select s.*, d.dept_no
from salaries as s
join dept_manager as d
on s.emp_no=d.emp_no #内连接,避免生成大表,先on满足条件再join,默认内连接,可以省略inner
where s.to_date = '9999-01-01' and d.to_date='9999-01-01'
#4、查找所有已经分配部门的员工的last_name、first_name以及dept_no
select e.last_name, e.first_name, d.dept_no
from dept_emp as d
inner join employees as e
on e.emp_no = d.emp_no
#5、查找所有员工的last_name和first_name以及对应部门编号dept_no,也包括暂时没有分配
#具体部门的员工
select e.last_name, e.first_name, d.dept_no
from empoyees e
left join dept_emp d #左连接以左表为主表,可以查询右表(dept_emp)中不存在的记录
on e.emp_no = d.emp_no
#6、查找所有员工入职的时候的薪水情况,给出emp_no以及salary,并按emp_no进行逆序
select e.emp_no, s.salary
from employees as e, salaries as s
where e.emp_no = s.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、找出所有员工当前具体的薪水salary情况,相同的薪水只显示一次,并逆序显示
select distinct salary 
from salaries
where to_date = '9999-01-01'
order by salary desc 
#9、获取所有部门当前manager的当前薪水,给出dept_no,emp_no以及salary
select d.dept_no, d.emp_no, s.salary
from salaries as s
inner join dept_maneger as d
on d.emp_no = s.emp_no
and d.to_date = '9999-01-01'
and s.to_date = '9999-01-01'
#10、获取所有非manager的员工emp_no
select emp_no
from employees
where emp_no not in (
	select emp_no from dept_manager)
#11、获取所有员工当前manager,如果员工manager的话不显示
select e.emp_no, m.emp_no as manager_no
from dept_emp as e
inner join dept_manager as m
on e.dept_no = m.dept_no
where m.todate = '9999-01-01'
and e.to_date = '9999-01-01'
and e.emp_to <> m.emp_no 
#12、获取所有部门中当前员工当前薪水最高的相关信息,给出dept_no,emp_no以及其对应的salary,
#按照部门升序排列
select d.dept_no, s.emp_no, max(s.salary) as salary
from salaries as s
inner join dept_emp as d
on d.emp_no = s.emp_no
where d.to_date = '9999-01-01' AND s.to_date = '9999-01-01'
group by d.dept_no
#13、从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t
select title, count(title) as t
from titles
group by title 
having t >= 2
#14、从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。
#注意对于重复的emp_no进行忽略
select title, count(distinct emp_no) as t
from titles
group by title
having t >= 2
#15、查找emoloyees表所有emp_no为奇数,且last_name不为Mary(注意大小写)的员工信息,
#并按照hire_date逆序排列
select * from employees
where last_name not like 'Mary'
and emp_no % 2 = 1
order by hire_date desc
#16、统计出当前各个title类型对应的员工当前薪水对应的平均工资。结果给出title以及
#平均工资avg。
select t.title, avg(s.salary)
from salaries as s
inner join titles as t
on s.emp_no = t.emp_no
and s.to_date = '9999-01-01'
and t.to_date = '9999-01-01'
group by title
#17、获取当前薪水第二多的员工的emp_no以及其对应的薪水salary
select emp_no, salary
from salaries
where salary = (
	select salary from salaries
    group by salary
    order by salary desc limit 1, 1)
and to_date = '9999-01-01'
#18、查找当前薪水排名第二多的员工编号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 s.to_date = '9999-01-01'
and s.salary not in (
	select max(salary) from salaries)
#19、查找所有员工的last_name和first_name以及对应的dept_name, 也包括暂时没有分配部门的员工
select em.last_name, em.first_name, dp.dept_name
from (employees as em left join dept_emp as de on em.emp_no = de.emp_no)
left join departments as dp
on de.dept_no = dp.dept_no
#20、查找员工编号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 asc limit 1)
    ) as growth
#21、查找所有员工自入职以来的薪水涨幅情况,给出员工编号emp_no以及其对应的薪水涨幅growth,并按照growth进行升序
select a.emp_no, (b.salary - c.salary) as growth
from employees as a
inner join salaries as b
on a.emp_no = b.emp_no and b.to_date = '9999-01-01' #查询当前的工资
inner join salaries as c
on a.emp_no = c.emp_no and a.hire_date = c.from_date #查询入职工资
order by growth asc
#22、统计各个部门的工资记录数,给出部门编码dept_no、各个部门dept_name以及部门在salaries表里面有多少调记录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 #尽量使用内连接来代替笛卡尔积连接,on在join之前执行,可是使查询的表更小,从而占用的内存空间更小。
#23、对所有员工的当前薪水按照salary进行按照1-N的排名,相同salary并列且按照emp_no升序排列
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
#24、获取所有非manager员工当前的薪水情况,给出dept_no、emp_no以及salary
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') #注意:on在join之前执行,所以节省内存,相比直接进行笛卡尔积
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当前薪水还高的相关信息,结果第一列给出员工的emp_no,第二列给出其manager的manager_no,第三列给出该员工当前的薪水emp_salary,第四列给出该员工对应的manager当前的薪水manager_salary
select e.emp_no, m.emp_no as manager_no, e.salary as emp_salary, m.salary as manager_salary
from 
(select s.salary, s.emp_no, de.dept_no from salariees s
inner join dept_emp de
on s.emp_no = de.emp_no and s.to_date = '9999-01-01') as e,
(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 m
where e.dept_no = m.dept_no
and e.salary > m.salary
#26、汇总各个部门当前员工的title类型的分配数目,即结果给出部门编号dept_no、dept_name、其部门下所有的当前员工的当前title以及该类型title对应的数目count
select de.dept_no, de.dept_name, t.title, count(t.title) as count #注意:虽然group by中没有de.dept_name,但是一个de.dept_no只对应一个de.dept_name
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逆序排列
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_dat2) - strftime("%Y", s1.from_date) = 1)
order by salary_growth desc
#28、将employees表的所有成员的list_name和first_name拼接起来作为Name,中间以一个空格区分
select last_name||" "||first_name as Name
from employees
#29、针对salaries表emp_no字段创建索引idx_emp_no,查询emp_no为10005,使用强制索引
select * from salaries
indexed by idx_emp_no
where emp_no = 10005
#30、创建一个触发器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 #触发器执行的内容写出 BEGIN与END 之间
    INSERT INTO audit VALUES (NEW.ID, NEW.NAME); #可以使用 NEW 与OLD 关键字访问触发后或触发前的记录
END;
#31、删除emp_no重复的记录,只保留最小的id对应的记录
delete from title_test where id not in
(select MIN(id) from title_test group by emp_no);
#32、将所有to_date为9999-01-01的全部更新为NULL,且from_date更新为2001-01-01
update title_test
set to_date = NULL, from_date = '2001-01-01'
where to_date = '9999-01-01';
#33、将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 title_test values(5, 10005, 'Senior Engineer', '1986-06-26', '9999-01-01');
#34、将title_test表名修改为titles_2017
alter table titles_test rename to titles_2017;
#35、在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));
#36、将所有获取奖金的员工当前的薪水增加10%
create table emp_bonus(
	emp_no int not null,
    btype smallint not null);
update salaries set salary = salary * 1.1
where emp_no in (select emp_no in emp_bonus)
and to_date = '9999-01-01';
#37、针对库中的所有表生成select count(*)对应的SQL语句
select "select count(*) from " || name || ";"
from sqlite_master
where type = 'table'
#38、将employees表中的所有员工的last_name和first_name通过(')连接起来
select last_name || "'" || first_name from employees
#39、获取employees中的first_name,查询按照first_name最后两个字母,按照升序进行排列
select first_name from employees 
order by substr(first_name, length(first_name) - 1)
#40、按照dept_no进行汇总,属于同一个部门的emp_no按照逗号进行连接,结果给出dept_no以及连接出的结果employees
select dept_no, group_concat(emp_no) as employees
from dept_emp
group by dept_no
#41、查询排除最大、最小salary之后的当前员工的平均工资avg_salary
select avg(salary) as avg_salary
from salaries
where to_date = '9999-01-01'
and salary not in(select max(salary) from salaries where to_date = '9999-01-01')
and salary not in(select min(salary) from salaries where to_date = '9999-01-01');
#42、分页查询employees表,每5行一页,返回第2页的数据
select * 
from employees
limit 5, 5;
#43、获取所有员工的emp_no、部门编号dept_no以及对应的bonus类型btype和received,没有分配奖金的员工不显示对应的bonus类型btype和received
select de.emp_no, de.dept_no, eb.btype, eb.received
from dept_emp as de
left join emp_bonus as eb
on de.emp_no = eb_emp_no
#44、使用含有关键字exists查找未分配具体部门的员工的所有信息
select * from employees where not exists
(select emp_ no from dept_emp where emp_no = employees.emp_no)
#45、获取有奖金的员工相关信息,给出emp_no、first_name、last_name、奖金类型btype、对应的当前薪水情况salary以及奖金金额bonus。
#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 as e
inner join emp_bonus as b
on e.emp_no = b.emp_no
inner join salaries as s
on e.emp_no = s.emp_no
and s.to_date = '9999-01-01'
#46、对于employees表中,输出first_name排名(按first_name升序排序)为奇数的first_name
select e1.first_name
from
(select e2.first_name, (select count(*) from employees as e3 where e3.first_name <= e2.first_name) as rowid
from employees as e2) as e1
where e1.rowid % 2 = 1
 
 
#电影系列
 
#电影表
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);
#1、查询描述信息中包括robot的电影对应的分类名称以及电影数目,而且还需要该分类包含电影总数量>=5部
select c.name, count(fc.film_id)
from
(select category_id, count(film_id) as category_num from
film_category 
group by category_id 
having count(film_id) >=5) as cc,
film as f, category as c, film_category as fc
where f.description like '%robot%'
and f.film_id = fc.film_id and fc.category_id = c.category_id
and c.category = cc.category_id
#2、使用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;
#3、使用子查询的方式找出属于Action分类的所有电影对应的title,description
select f.title, f.description
from film as f
where f.film_id in
(select fc.film_id from film_category as fc where fc.category_id in
(select c.category_id from category as c where c.name = 'Action'));
#4、创建一个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')));
#5、对于表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
insert into actor values
(1, 'PENELOPE', 'GUINESS', '2006-02-15 12:34:33'),
(2, 'NICK', 'WAHLBERG', '2006-02-15 12:34:33');
#6、对于表actor批量插入如下数据,如果数据已经存在,请忽略
-- 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');
#7、创建一个actor_name表,并且将actor表中的所有first_name以及last_name导入该表
-- 列表			类型			是否为NULL		含义
-- first_name	varchar(45)		not null		名字
-- last_name	varchar(45)		not null		姓氏
create table actor_name as
select first_name, last_name from actor;
#8、针对表actor,对first_name创建唯一索引uniq_idx_firstname,对last_name创建普通索引idx_lastname
create unique index uniq_idx_firstname on actor(firs_tname);
create index idx_lastname on actor(last_name);
#9、针对actor表创建视图actor_name_view,只包含first_name以及last_name两列,并对这两列重新命名,first_name为first_name_v,last_name为last_name_v
create view actor_name_view as
select first_name as first_name_v, last_name as last_name_v
from actor
#10、在actor表的last_update后面新增加一列名字为create_date,类型为datetime,not null,默认值为'0000-00-00 00:00:00'
alter table actor
add `create_date` datetime not null default '0000-00-00 00:00:00' 
 
 
  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2022-04-18 17:49:30  更:2022-04-18 17:53:40 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 3:44:17-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码