hadoop 实战练习_hadoop入门到实战(2)hive经典练习题
点击上方蓝字关注我们
一.建表和加载数据
1.student表
create table if not exists student(s_id int,s_name string,s_birth string,s_sex string)
row format delimited
fields terminated by ','
;
load data local inpath '/root/data.txt' into table student;
2.course表
create table if not exists course(c_id int,c_course string,t_id int)
row format delimited
fields terminated by ','
;
load data local inpath '/root/data.txt' into table course;
3.teacher表
create table if not exists teacher(t_id int,t_name string)
row format delimited
fields terminated by ','
;
load data local inpath '/root/data.txt' into table teacher;
4.score表
create table if not exists score(s_id int,c_id int, s_score DOUBLE)
row format delimited
fields terminated by ','
;
load data local inpath '/root/data.txt' into table score;
二.查询"01"课程比"02"课程成绩高的学生的信息及课程分数?
答案①:
select stu.*,c.*
from student stu
join score a on a.c_id = '01' and a.s_id= stu.s_id
left join score b on b.c_id = '02' and b.s_id= stu.s_id
join score c on c.s_id= stu.s_id
where a.s_score > b.s_score or b.s_score is null
;
答案②:
select stu.*,c.*
from student stu
left join score a on a.c_id = '02' and a.s_id= stu.s_id
join score b on b.c_id = '01' and b.s_id= stu.s_id
join score c on c.s_id= stu.s_id
where a.s_score < b.s_score or a.s_score is null
;
三.查询"01"课程比"02"课程成绩低的学生的信息及课程分数:
答案①:
select stu.*,c.*
from student stu
join score a on a.c_id = '02' and a.s_id= stu.s_id
left join score b on b.c_id = '01' and b.s_id= stu.s_id
join score c on c.s_id= stu.s_id
where a.s_score > b.s_score or b.s_score is null
;
答案②:
select stu.*,c.*
from student stu
left join score a on a.c_id = '01' and a.s_id= stu.s_id
join score b on b.c_id = '02' and b.s_id= stu.s_id
join score c on c.s_id= stu.s_id
where a.s_score < b.s_score or a.s_score is null
;
总结:对于二题和三题的查询连接的方法:谁大就把谁放在左边,谁小就把谁舍弃。
四.查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩?
答案①:
select
a.s_id,stu.s_name,avg(a.s_score) as avgscore
from score a
join student stu on a.s_id = stu.s_id
group by a.s_id,stu.s_name
having avgscore >= 60
;
答案②:
select
a.s_id,stu.s_name,avg(a.s_score)>=60
from score a
join student stu on a.s_id = stu.s_id
group by a.s_id,stu.s_name
;
五.查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩?
答案①:
select
a.s_id,stu.s_name,avg(a.s_score) as avgscore
from score a
join student stu on a.s_id = stu.s_id
group by a.s_id,stu.s_name
having avgscore < 60
union all
select stu.s_id,stu.s_name,NULL as avgscore
from student stu
left join score a on stu.s_id = a.s_id
where a.s_score is null
;
六.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
答案:
select stu.s_id,stu.s_name,count(sc.s_id) as totalSubjects,sum(sc.s_score) as sumScores
from student stu left join score sc on stu.s_id=sc.s_id
group by stu.s_id,stu.s_name;
七.查询"李"姓老师的数量?
select count(1)from teacher where t_name like '李%';
八.查询学过"张三"老师授课的同学的信息?
select distinct stu.*
from student stu
join score sc on stu.s_id=sc.s_id
join course co on sc.c_id=co.c_id
join teacher te on co.t_id =te.t_id
where te.t_name='张三';
九.查询没学过"张三"老师授课的同学的信息?
select *
from student stu
join teacher te on te.t_name='张三'
join course co on te.t_id=co.t_id
left join score sc on stu.s_id=sc.s_id and co.c_id=sc.c_id
where sc.s_score is null;
十.查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息?
select stu.*,sc.*
from student stu,score sc,score sc1
where stu.s_id=sc.s_id and stu.s_id=sc1.s_id
and sc.c_id=1 and sc1.c_id=2;
十一.查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息:
select stu.*
from student stu
join score sc on sc.s_id =stu.s_id and sc.c_id = '01'
where not exists (select 1 from score sc1 where sc1.c_id = '02' and stu.s_id = sc1.s_id)
;
十二.查询没有学全所有课程的同学的信息?
select distinct stu.*
from student stu
join score sc
left join course co
on stu.s_id=sc.s_id and sc.c_id=co.c_id
where sc.s_score is null;
十三.查询至少有一门课与学号为"01"的同学所学相同的同学的信息?
select distinct stu.*
from student stu
join score sc on stu.s_id=sc.s_id
where stu.s_id <> 1 and sc.c_id in
(select c_id from score where s_id=1);
十四.查询和"01"号的同学学习的课程完全相同的其他同学的信息?
select
十五.查询没学过"张三"老师讲授的任一门课程的学生姓名?
select stu.*
from student stu
join teacher te on te.t_name = '张三'
join course co on co.t_id = te.t_id
left join score sc on sc.c_id = co.c_id and sc.s_id = stu.s_id
where sc.s_score is null;
十六.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩?
select
*
from student stu
join score sc on sc.s_id = stu.s_id
where sc.s_score < 60
;
十七.检索"01"课程分数小于60,按分数降序排列的学生信息?
select *
from student stu
join score sc on sc.s_id = stu.s_id
where sc.c_id = 1 and sc.s_score < 60
order by sc.s_score desc;
十八.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩?
select *,
round(avg(sc.s_score) over(distribute by sc.s_id),2) as avg1
from score sc
order by avg1 desc,sc.s_score desc;
总结:在这里啊,round是hive的内置函数,其功能是四舍五入。
十九..查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率:
喜欢就点个在看再走吧
hadoop 实战练习_hadoop入门到实战(2)hive经典练习题相关推荐
- 【Zookeeper实战】Zookeeper入门到实战看这篇就够了
1. 前言 在上一篇[Zookeeper入门]相关概念总结 中已经完美的讲解了 Zookeeper入门 相关概念总结,接下来讲讲ZooKeeper 实战使用. 这篇文章简单给演示一下 ZooKeepe ...
- javascript实战pdf_web前端入门到实战:10分钟入门 CSS3 Animation
Animation可以让你不用依赖javascript或jquery,用纯CSS在网页中轻松实现各种动画效果. 兼容性 animation在绝大部分主流浏览器都得到了很好的支持!还在兼容IE9的同学要 ...
- 【RNN从入门到实战】GRU入门到实战——使用GRU预测股票。
摘要 GRU是LSTM网络的一种效果很好的变体,它较LSTM网络的结构更加简单,而且效果也很好,因此也是当前非常流形的一种网络.GRU既然是LSTM的变体,因此也是可以解决RNN网络中的长依赖问题. ...
- webapp入门到实战_web前端入门到实战:前端高手在CSS 开发效率的必备片段
这篇文章会记录我们平时常用到的 CSS 片段,使用这些 CSS 可以帮助我们解决许多实际项目问题中遇到的,墙裂建议点赞收藏再看,方便日后查找 清除浮动 浮动给我们的代码带来的麻烦,想必不需要多说,我们 ...
- python练习题及答案-听说你python基础入门了?100个经典练习题送给你(附完整答案)...
我一开始很喜欢python的哲学,然后买了一个<python学习手册>,简单的了解了一下python 不过因为没什么实际的需求,只是了解,后来自己做网站用了Django,然后又做了一个网站 ...
- 《黑马程序员2023新版黑马程序员大数据入门到实战教程,大数据开发必会的Hadoop、Hive,云平台实战项目》学习笔记总目录
本文是对<黑马程序员新版大数据入门到实战教程>所有知识点的笔记进行总结分类. 学习视频:黑马程序员新版大数据 学习时总结的学习笔记以及思维导图会在后续更新,请敬请期待. 前言:配置三台虚拟 ...
- 《 Docker 技术入门与实战 》读书笔记 ( CentOS 安装 Docker )
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. PS :个人所有读书笔记只记录个人想要的内容,很可能原书大量内容没有纳入笔记中... ... 以下全 ...
- 推荐系统从入门到实战笔记
文章目录 推荐系统从入门到实战 1. 推荐系统包含哪些环节 2. 推荐系统有哪些召回路径 3. Netflix经典的推荐系统架构 4. 推荐系统通用架构图(数据流图) 5. 推荐系统如何实现多路召回的 ...
- 大数据从入门到实战 - 第3章 MapReduce基础实战
大数据从入门到实战 - 第3章 MapReduce基础实战 一.关于此次实践 1.实战简介 2.全部任务 二.实践详解 1.第 1 关:成绩统计 2.第 2 关:文件内容合并去重 3.第 3 关:信息 ...
最新文章
- iframe怎么用_怎么样减少无效URL的爬行和索引
- U盘如何量产成USB-CDROM
- Spring Data JPA
- Bitmap和YUV的转换
- 在python中配置MySQL数据库
- Python之GUI:基于Python的GUI界面设计的一套AI课程学习(机器学习、深度学习、大数据、云计算等)推荐系统(包括语音生成、识别等前沿黑科技)
- EasyUI表单验证,自定义插件验证,自定义js插件验证,远程验证,常见手机号,中英文,qq等验证规则验证
- PHP JSON数组与对象的理解
- 用gSOAP开发Web Service程序
- 西门子宣布美国充电桩扩产计划
- Bash漏洞引发僵尸网络狂欢
- JPA学习笔记---JPA实体Bean的建立---链接上一个博文:对实体Bean中属性进行操作:保存日期类型,设置字段的长度,名字,是否为空,可以声明枚举字段;可以存放二进制数据,可以存放
- 线性搜索c语言,线性搜索实例程序(C语言)
- Spark on yarn 动态资源配置
- linux关闭邮件提示错误,LINUX命令关闭 You have mail in /var/spool/mail/root邮件提醒功能...
- SSM毕设项目车辆维修管理系统m97p7(java+VUE+Mybatis+Maven+Mysql)
- 如何检查python安装成功_如何检查python是否安装成功
- css通用命名大全,CSS的常用命名及规范
- java 字节码查看_一种查看java字节码时显示方法调用关系图的方法与流程
- SHT30 温湿度传感器 使用示例
热门文章
- 电视机当计算机屏幕,怎么实现电视机当电脑的显示器和音箱用?
- 苏州大学9月计算机考试试题,2016年9月计算机一级考试题及答案
- fireworks做图的最高长度
- mxnet转pytorch预训练
- “pybind11::module_::def”: 未找到匹配的重载函数
- Windows 快速删除 大量文件
- PowerShell因为在此系统中禁止执行脚本
- you're probably running inside a thread without first calling pythoncom.CoInitialize
- libtorch error C1021: 无效的预处理器命令“warning”
- name 'false' is not defined