1 Star 0 Fork 0

longpahu / bigdata

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
hive_homework.sql 3.94 KB
一键复制 编辑 原始数据 按行查看 历史
longpahu 提交于 2023-01-24 11:36 . add hive homework
查询"01"课程比"02"课程成绩高的学生的信息及课程分数
select stu.*, s1.s_score, s2.s_score from student stu
join score s1 on stu.s_id = s1.s_id and s1.c_id = '01'
join score s2 on stu.s_id = s2.s_id and s2.c_id = '02'
where s1.s_score > s2.s_score;
查询"01"课程比"02"课程成绩低的学生的信息及课程分数
select stu.*, s1.s_score, s2.s_score from student stu
join score s1 on stu.s_id = s1.s_id and s1.c_id = '01'
join score s2 on stu.s_id = s2.s_id and s2.c_id = '02'
where s1.s_score < s2.s_score;
查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select stu.s_id, stu.s_name, b.avg_score from student stu join
(select s_id, avg(s_score) as avg_score from score group by s_id having avg(s_score) >= 60) b on stu.s_id = b.s_id;
查询平均成绩小于 60 分的同学的学生编号和学生姓名和平均成绩 (包括有成绩的和无成绩的)
select stu.s_id, stu.s_name, b.avg_score from student stu join
(select s_id, avg(s_score) as avg_score from score group by s_id having avg(s_score) < 60) b on stu.s_id = b.s_id;
查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select stu.s_id, stu.s_name, b.course_count, b.total_score from student stu join (select s_id, count(*) as course_count, sum(s_score) as total_score from score group by s_id) b on stu.s_id = b.s_id;
查询"李"姓老师的数量
select count(*) from teacher where t_name like '%李%';
查询学过"张三"老师授课的同学的信息
select * from student where exists (select * from score join course on score.c_id = course.c_id join teacher on course.t_id = teacher.t_id and t_name = '张三' where score.s_id = student.s_id);
查询没学过"张三"老师授课的同学的信息
select * from student where not exists (select * from score join course on score.c_id = course.c_id join teacher on course.t_id = teacher.t_id and t_name = '张三' where score.s_id = student.s_id);
查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
select * from student where
exists (select * from score where c_id = '01' and score.s_id = student.s_id) and
exists (select * from score where c_id = '02' and score.s_id = student.s_id);
查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
select * from student where
exists (select * from score where c_id = '01' and score.s_id = student.s_id) and
not exists (select * from score where c_id = '02' and score.s_id = student.s_id);
查询没有学全所有课程的同学的信息
select * from student where s_id in (select s_id from score group by s_id having count(*) < (select count(*) from course) );
查询至少有一门课与学号为"01"的同学所学相同的同学的信息
select student.* from student
join (select c_id from score where score.s_id=01)tmp1
join (select s_id,c_id from score)tmp2
on tmp1.c_id =tmp2.c_id and student.s_id =tmp2.s_id
where student.s_id not in('01')
group by student.s_id,s_name,s_birth,s_sex;
查询和"01"号的同学学习的课程完全相同的其他同学的信息
select student.*, tmp1.course_id_other from student
join (select s_id ,concat_ws('|', collect_set(c_id)) course_id_other from score where s_id != '01' group by s_id)tmp1 on student.s_id = tmp1.s_id
join (select concat_ws('|', collect_set(c_id)) course_id_01 from score where s_id = '01') tmp2 on tmp1.course_id_other = tmp2.course_id_01;
查询没学过"张三"老师讲授的任一门课程的学生姓名
select * from student where not exists
(select * from score join course on score.c_id = course.c_id join teacher on course.t_id = teacher.t_id where score.s_id = student.s_id and teacher.t_name = '张三')
查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select student.s_id, student.s_name, tmp.avg_score from student join
(select s_id, avg(s_score) as avg_score from score where s_score < 60 group by s_id having count(*) >= 2) tmp on student.s_id = tmp.s_id;
1
https://gitee.com/longpahu/bigdata.git
git@gitee.com:longpahu/bigdata.git
longpahu
bigdata
bigdata
master

搜索帮助