一.建表和加载数据
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,最高分,最低分,平均分,及格率,中等率,优良率,优秀率:
-- 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90?
select
co.c_id,
co.c_course,
max(sc.s_score),
min(sc.s_score),
round(avg(sc.s_score),3),
round(sum(case when sc.s_score >=60 then 1 else 0 end)/count(1) *100,3) as `及格率`,
round(sum(case when sc.s_score between 70 and 79 then 1 else 0 end)/count(1) *100,3) as `中等率`,
round(sum(case when sc.s_score between 80 and 89 then 1 else 0 end)/count(1) *100,3) as `优良率`,
round(sum(case when sc.s_score>=90 then 1 else 0 end)/count(1) *100,3) as `优秀率`
from score sc
join course co on sc.c_id=co.c_id
group by co.c_id,co.c_course; 二十.按各科成绩进行排序,并显示排名:– row_number() over()分组排序功能?
select *,
row_number() over(distribute by c_id sort by s_score desc) from score;
二十一.查询学生的总成绩并进行排名?
select s_id,sum(s_score) as sumScores
from score
group by s_id
order by sumScores desc;
二十二:查询不同老师所教不同课程平均分从高到低显示?
select t_id,sc.c_id,round(avg(sc.s_score),2) as avgscore
from score sc
join course co on sc.c_id=co.c_id
group by t_id,sc.c_id
order by t_id,avgscore desc;
二十三.查询所有课程的成绩第2名到第3名的学生信息及该课程成绩?
select *
from ( select *,
row_number() over(distribute by c_id sort by s_score desc) as rm
from score ) a
where a.rm between 2 and 3;
二十四.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比?
select c_id,
sum(case when s_score>=85 then 1 else 0 end) as 85score,
sum(case when s_score between 70 and 84 then 1 else 0 end) as 70score,
sum(case when s_score between 60 and 69 then 1 else 0 end) as 60score,
sum(case when s_score<60 then 1 else 0 end) as 0score,
count(1) as totalscore
from score group by c_id;
二十五.查询学生平均成绩及其名次?
select *,
row_number() over(sort by a.avgscore desc) as rm
from (
select s_id,round(avg(s_score),2) as avgscore
from score group by s_id) a;
二十六.查询各科成绩前三名的记录三个语句?
select *
from (
select *,
row_number() over(distribute by c_id sort by s_score desc) as rm,
rank() over(distribute by c_id sort by s_score desc) as rk,
dense_rank() over(distribute by c_id sort by s_score desc) as drk
from score) a where a.rm<4;
二十七.查询每门课程被选修的学生数?
select c_id,count(1) as `学生人数`
from score
group by c_id;
二十八.查询出只有两门课程的全部学生的学号和姓名?
select stu.s_id,stu.s_name
from student stu
join score sc on sc.s_id=stu.s_id
group by stu.s_id,stu.s_name
having count(1)=2;
二十九.查询男生、女生人数?
select s_sex,count(1) as totalstu
from student
group by s_sex;
三十.查询名字中含有"风"字的学生信息?
select *
from student
where s_name like '%风%' ;
三十一.查询同名同性学生名单,并统计同名人数?
select s_name,s_sex,count(1) as totalstu
from student
group by s_name,s_sex
having totalstu>1;
三十二.查询1990年出生的学生名单?
select *
from student
where s_birth like '1990%';
select *
from student
where substr(s_birth,0,4)='1990';
三十三.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列?
select c_id,round(avg(s_score),2) as avgscore
from score
group by c_id
order by avgscore desc,c_id asc;
三十四:查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩?
select stu.s_name,avg(sc.s_score) as avgscore
from student stu
join score sc on stu.s_id=sc.s_id
group by stu.s_id,stu.s_name
having avgscore>85;
三十五:查询课程名称为"数学",且分数低于60的学生姓名和分数?
select stu.s_name,sc.s_score
from student stu
join score sc on stu.s_id=sc.s_id
join course co on sc.c_id=co.c_id and co.c_course='数学'
where sc.s_score<60;
三十六:查询所有学生的课程及分数情况?
select *
from student stu
join score sc on stu.s_id=sc.s_id
right join course co on sc.c_id=co.c_id
;
三十七:查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数?
select stu.s_name,co.c_course,sc.s_score
from student stu
join score sc on stu.s_id=sc.s_id
join course co on sc.c_id=co.c_id
group by stu.s_id,stu.s_name,co.c_course,sc.s_score having min(sc.s_score)>=70;
三十八:查询课程不及格的学生?
select stu.*
from student stu
join score sc on stu.s_id=sc.s_id
where sc.s_score<60;
三十九:查询课程编号为01且课程成绩在80分以上的学生的学号和姓名?
select stu.s_id,stu.s_name,sc.c_id,sc.s_score
from student stu
join score sc on stu.s_id=sc.s_id
where c_id=1 and s_score>=80;
四十:每门课程的学生人数?
select sc.c_id,co.c_course,count(1) as stunum
from score sc
join course co on sc.c_id=co.c_id
group by sc.c_id,co.c_course;
四十一:查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩?
select *
from (
select
dense_rank() over(distribute by sc.c_id sort by sc.s_score desc) drk
from score sc
join course co on sc.c_id=co.c_id
join teacher te on co.t_id=te.t_id
where te.t_name='张三') aa
where aa.drk=1;
四十二:查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩?
①select distinct sc.s_id,sc.c_id,sc.s_score
from score sc,score sc1
where sc.c_id<>sc1.c_id and sc.s_score=sc1.s_score and sc.s_id=sc1.s_id;
②select distinct sc.s_id,sc.c_id,sc.s_score
from score sc,score sc1
where sc.c_id !=sc1.c_id and sc.s_score=sc1.s_score and sc.s_id=sc1.s_id;
四十三:查询每门课程成绩最好的前三名?
select *
from (select *,
row_number() over(distribute by c_id sort by s_score desc) rn
from score) aa
where aa.rn<=3;
四十四:统计每门课程的学生选修人数(超过5人的课程才统计)?
select c_id,count(*) as stunum
from score
group by c_id
having stunum>5
order by c_id asc,stunum desc;
四十五:检索至少选修两门课程的学生学号?
select s_id,count(1) as coursenum
from score
group by s_id having coursenum >=2;
四十六:查询选修了全部课程的学生信息?
select stu.s_id,stu.s_name
from student stu
join score sc on stu.s_id=sc.s_id
left join course co on sc.c_id=co.c_id
group by stu.s_id,stu.s_name
having sum(case when sc.s_score is null then 1 else 0 end)=0;
四十七:查询各学生的年龄(周岁)?
select
s_birth,
year(current_date())-year(s_birth)-
(case
when month(current_date())>month(s_birth)
then 0
when month(current_date())=month(s_birth)
and
day(current_date())>=day(s_birth)
then 0 else 1 end)
from student;
四十八:查询本周过生日的学生?
select *
from student
where weekofyear(s_birth)=weekofyear(current_date());
四十九:查询下周过生日的学生?
select *
from student
where weekofyear(s_birth)=weekofyear(current_date())+1;
四十九:查询上周过生日的学生?
select *
from student
where weekofyear(s_birth)=weekofyear(current_date())-1;
五十:查询本月过生日的学生?
select *
from student
where month(s_birth)=month(current_date());
五十:查询上月过生日的学生?
select *
from student
where month(s_birth)=month(current_date())-1;
五十:查询下月过生日的学生?
select *
from student
where month(s_birth)=month(current_date())+1;
五十一:查询12月份过生日的学生?
①select *
from student
where month(s_birth)=12; ②select *
from student
where substring(s_birth,4,2)=12;

hive经典练习题的更多相关文章

  1. Python经典练习题1:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

    Python经典练习题 网上能够搜得到的答案为: for i in range(1,85): if 168 % i == 0: j = 168 / i; if i > j and (i + j) ...

  2. 【视频+图文】Java基础经典练习题(一)输出2-100之间的素数,及素数个数

    目录 第一题:判断2-100之间有多少个素数,并输出所有素数. 1.视频讲解: 2.思路分析: 代码讲解:以i=4为例 4.为大家准备了彩蛋: 能解决题目的代码并不是一次就可以写好的 我们需要根据我们 ...

  3. MYSQL经典练习题,熟悉DQL

    MYSQL经典练习题 (本练习题可让你熟悉DQL,快速的上手DQL) 首先,先在数据库中建立基本数据库以及表项: DROP DATABASE IF EXISTS `test`; CREATE DATA ...

  4. SQL 经典练习题

    create database 练习题gouse 练习题go create table Student( Sno char(3) primary key, Sname char(8) not null ...

  5. python基础阶段 经典练习题 拾英札记(2)

    因为编程的练习题是交互式的,在不断调试和不断渐进完善中,你会有一种成就感和快乐感,不断的修缮,不断的尝试. 其实,认知自己,和探索世界,也是这样的啊. 只要不放弃,要坚持. #7  根据列表lt,实现 ...

  6. MySQL经典练习题

    表名和字段 –1.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 –2.课程表 Course(c_id,c_name,t_id ...

  7. SQL经典练习题50--mysql

    --1.学生表 Student(Sid,Sname,Sage,Ssex)? --Sid 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别 --2.课程表? Course(Cid, ...

  8. MySQL经典练习题及答案,常用SQL语句练习50题

    表名和字段 –1.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别 –2.课程表 Course(c_id,c_name,t_id ...

  9. c#基础——for循环嵌套经典练习题(打★)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 作业题1 ...

随机推荐

  1. Flask基础-01.Flask简介

    Flask简介 Web应用程序作用 Web(World Wide Web)诞生最初的目的,是为了利用互联网交流工作文档. 关于Web框架 1. 什么是Web框架? 1. 已经封装好了一段代码,协助程序 ...

  2. Numpy学习-(2)

    我学习numpy过程的记录 1. 切片和索引 (1) 两种切片方式示例: (2) 多维数组: import numpy as np a = np.array([[1,2,3],[3,4,5],[4,5 ...

  3. C#开发BIMFACE系列32 服务端API之模型对比3:批量获取模型对比状态

    系列目录     [已更新最新开发文章,点击查看详细] 在<C#开发BIMFACE系列31 服务端API之模型对比2:获取模型对比状态>中介绍了根据对比ID,获取一笔记录的对比状态.由于模 ...

  4. 关于MIME类型问题,浏览器请求到的资源是乱码

    简介 我想很多同学都可能会遇到这样的问题,调用后台提共的静态资源服务api时,用浏览器打开发现却是一堆乱码.需要的是 JSON, 拿到的却是 xml,访问一个mp4的文件,浏览器直接下载.这一切的来源 ...

  5. ADO.Net和Entity Framework的区别联系

    它们有以下几点区别:1,ADO.Net是开发人员自己select.update等写sql语句,来实现对数据库的增删改查等操作:采用EF进行开发操作数据库的时候,只需要操作对象,这样做使开发更方便,此时 ...

  6. PHP open_basedir配置未包含upload_tmp_dir 导致服务器不能上传文件

    在做一个上传图片的功能时候发现后台接收到的$_FILES['file']['error'] = 6,这个错误意思是找不到临时文件,或者是临时文件夹无权限,需要更改php.ini文件的 upload_t ...

  7. phpcms 后台分页

    php 代码public function init() { $where='';//条件 $page = $_GET['page'] ? intval($_GET['page']) : '1'; $ ...

  8. 如何给 Inno Setup 生成的安装包添加版本信息

    使用 Inno 已有的函数 GetFileVersion 获取 EXE 文件的版本 #define ApplicationName 'Application Name' #define Applica ...

  9. Libra教程之:Libra protocol的逻辑数据模型

    文章目录 Libra protocol简介 逻辑数据模型 账本状态 交易 账本历史 Libra protocol简介 Libra区块链本质上是一个加密数据库,这个数据库是通过Libra protoco ...

  10. Python3的日期和时间

    2019独角兽企业重金招聘Python工程师标准>>> python 中处理日期时间数据通常使用datetime和time库 因为这两个库中的一些功能有些重复,所以,首先我们来比较一 ...