Hive语句综合练习

举报
bigdata张凯翔 发表于 2021/03/26 00:52:25 2021/03/26
【摘要】 经典SQL练习题 第一步区:创建mysql表 第二步:导入数据到mysql里面去 第三步:创建hive表与myslq表字段对应 第四步:导出myslq表的数据,加载到hive表里面去 第五步:翻译需求 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数 SELECT a.* ,b.s_score AS 01_score,c.s_score AS 02_score F...

经典SQL练习题
第一步区:创建mysql表
第二步:导入数据到mysql里面去
第三步:创建hive表与myslq表字段对应
第四步:导出myslq表的数据,加载到hive表里面去

第五步:翻译需求

1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
SELECT a.* ,b.s_score AS 01_score,c.s_score AS 02_score FROM student a LEFT JOIN score b ON a.s_id=b.s_id AND b.c_id='01' LEFT JOIN score c ON a.s_id=c.s_id AND c.c_id = '02' WHERE b.s_score>c.s_score;
2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
SELECT a.* ,b.s_score AS 01_score,c.s_score AS 02_score FROM student a LEFT JOIN score b ON a.s_id=b.s_id AND b.c_id='01' JOIN score c ON a.s_id=c.s_id AND c.c_id='02' WHERE b.s_score<c.s_score;
3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
SELECT b.s_id,b.s_name,ROUND(AVG(a.s_score),2) AS avg_score FROM student b JOIN score a ON b.s_id = a.s_id GROUP BY b.s_id,b.s_name HAVING ROUND(AVG(a.s_score),2)>=60;
4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩

 -- (包括有成绩的和无成绩的)

SELECT b.s_id,b.s_name,ROUND(AVG(a.s_score),2) AS avg_score FROM student b LEFT JOIN score a ON b.s_id = a.s_id GROUP BY b.s_id,b.s_name HAVING ROUND(AVG(a.s_score),2)<60 UNION ALL SELECT a.s_id,a.s_name,0 AS avg_score FROM student a WHERE a.s_id NOT IN ( SELECT DISTINCT s_id FROM score);
-- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
SELECT a.s_id,a.s_name,COUNT(b.c_id) AS sum_course,SUM(b.s_score) AS sum_score FROM student a LEFT JOIN score b ON a.s_id=b.s_id GROUP BY a.s_id,a.s_name;

-- 6、查询"李"姓老师的数量
select count(t_id) from techer where t_name like '李%';
-- 7、查询学过"张三"老师授课的同学的信息
SELECT a.* FROM student a LEFT JOIN score b ON a.s_id = b.s_id WHERE b.c_id IN ( SELECT c.c_id FROM course c LEFT JOIN techer t ON c.t_id = t.t_id WHERE t.t_name = '张三' ) ;
-- 8、查询没学过"张三"老师授课的同学的信息
SELECT s.* FROM student s LEFT JOIN ( SELECT a.s_id FROM student a LEFT JOIN score b ON a.s_id = b.s_id WHERE b.c_id IN ( SELECT c.c_id FROM course c LEFT JOIN techer t ON c.t_id = t.t_id WHERE t.t_name = '张三' ) ) ss ON s.s_id = ss.s_id WHERE ss.s_id IS NULL;
-- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
select a.* from student a,score b,score c where a.s_id = b.s_id and a.s_id = c.s_id and b.c_id='01' and c.c_id='02';
-- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
SELECT qq.* FROM ( SELECT s.* FROM student s LEFT JOIN score sco ON s.s_id = sco.s_id LEFT JOIN course c ON sco.c_id = c.c_id WHERE c.c_id='01' ) qq LEFT JOIN ( SELECT stu.* FROM student stu LEFT JOIN score mysco ON stu.s_id = mysco.s_id LEFT JOIN course cou ON mysco.c_id = cou.c_id WHERE cou.c_id='02' ) pp ON qq.s_id = pp.s_id WHERE pp.s_id IS NULL;
-- 11、查询没有学全所有课程的同学的信息
SELECT ss.s_id FROM ( SELECT stu.s_id,COUNT(stu.s_id) AS num FROM student stu LEFT JOIN score sco ON stu.s_id = sco.s_id LEFT JOIN course cou ON sco.c_id = cou.c_id GROUP BY stu.s_id ) ss WHERE ss.num < 3
-- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
SELECT stu.* FROM student stu LEFT JOIN ( SELECT s.s_id FROM score s WHERE s.c_id IN( SELECT c_id FROM score WHERE s_id = '01' )GROUP BY s_id ) pp ON stu.s_id = pp.s_id WHERE pp.s_id IS NOT NULL;

文章来源: www.jianshu.com,作者:百忍成金的虚竹,版权归原作者所有,如需转载,请联系作者。

原文链接:www.jianshu.com/p/4876ab183894

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。