select*from student_info where stu_name like"%安%";
查询姓“王”老师的个数
selectcount(1)from db_hive.teacher_info where tea_name like'王%';
检索课程编号为“04”且分数小于60的学生的分数信息,结果按分数降序排列
select*from score_info where course_id='04'and score<60orderby score desc;
查询数学成绩75以上的学生信息和其对应的语文学科成绩,按照学号升序排序
select t1.*, t2.score
from student_info t1
leftjoin score_info t2 on t1.stu_id = t2.stu_id
where t2.course_id =(selectdistinct course_id from course_info where course_name ='语文')and t2.score >75orderby t1.stu_id;
select stu_id,sum(score) sum_score
from score_info
groupby stu_id
orderby sum_score desc;
查询一共参加三门课程且其中一门为语文课程的学生的id和姓名
select s1.stu_id, s1.stu_name
from(select stu_id
from score_info
where stu_id in(select stu_id
from score_info
where course_id =(select course_id
from course_info
where course_name ='语文'))groupby stu_id
havingcount(score)=3) s0
innerjoin student_info s1
on s0.stu_id = s1.stu_id;
复杂查询
查询没有学全所有课的学生的学号、姓名
select sti.stu_id,
sti.stu_name,count(sci.course_id)from student_info sti
leftjoin
score_info sci
on
sti.stu_id = sci.stu_id
groupby sti.stu_id, sti.stu_name
havingcount(sci.course_id)<(selectcount(*)from course_info)
查询出只选修了三门课程的全部学生的学号和姓名
select s0.stu_id, s0.stu_name
from student_info s0
leftjoin score_info s1
on s0.stu_id = s1.stu_id
groupby s0.stu_id,s0.stu_name
havingcount(s1.course_id)=3;
多表查询
查询所有学生的学号、姓名、选课数、总成绩
select s1.stu_id,
s1.stu_name,count(s2.course_id) count_course,sum(s2.score) sum_score
from student_info s1
leftjoin score_info s2
on s1.stu_id = s2.stu_id
groupby s1.stu_id, s1.stu_name;
查询平均成绩大于85的所有学生的学号、姓名和平均成绩
select s1.stu_id,
s1.stu_name,avg(score) avg_score
from student_info s1
leftjoin score_info s2
on s1.stu_id = s2.stu_id
groupby s1.stu_id,
s1.stu_name
havingavg(score)>85;
查询学生的选课情况:学号,姓名,课程号,课程名称
select s1.stu_id, s1.stu_name, c1.course_id, c1.course_name
from student_info s1
leftjoin score_info s2
on s1.stu_id = s2.stu_id
leftjoin course_info c1
on s2.course_id = c1.course_id;
查询课程编号为03且课程成绩在80分以上的学生的学号和姓名及课程信息
select s1.stu_id, s1.stu_name, c1.course_id, c1.course_name
from student_info s1
innerjoin(select*from score_info
where score >80and course_id ='03') s2
on s1.stu_id = s2.stu_id
innerjoin course_info c1
on s2.course_id = c1.course_id;
多表连接
课程编号为"01"且课程分数小于80,按分数降序排列的学生信息
select s1.stu_id,
s1.stu_name,
s1.sex,
s1.birthday,
s2.score
from student_info s1
innerjoin score_info s2
on s1.stu_id = s2.stu_id
where s2.course_id ='01'and s2.score <80orderby s2.score desc;
查询所有课程成绩在70分以上的学生的姓名、课程名称和分数,按分数升序排列
select s1.stu_name, c1.course_name, s2.score
from student_info s1
innerjoin score_info s2
on s1.stu_id = s2.stu_id
innerjoin(select stu_id from score_info
groupby stu_id
havingsum(if(score>70,0,1))=0) s3
on s1.stu_id=s3.stu_id
innerjoin course_info c1
on s2.course_id = c1.course_id
orderby s2.score;
查询该学生不同课程的成绩相同的学生编号、课程编号、学生成绩
select s1.stu_id, s2.course_id, s1.score
from score_info s1
innerjoin score_info s2
on s1.stu_id = s2.stu_id
and s1.course_id <> s2.course_id
and s1.score = s2.score;
查询课程编号为“01”的课程比“02”的课程成绩高的所有学生的学号
select s1.stu_id
from score_info s1
innerjoin score_info s2
on s1.stu_id = s2.stu_id
and s1.course_id ='01'and s2.course_id ='02'and s1.score > s2.score;
查询学过编号为“01”的课程并且也学过编号为“02”的课程的学生的学号、姓名
select s1.stu_id, s1.stu_name
from student_info s1
innerjoin score_info s2
on s1.stu_id = s2.stu_id
innerjoin score_info s3
on s1.stu_id = s3.stu_id
where s2.course_id ='01'and s3.course_id ='02';
查询学过“王子文”老师所教的所有课的同学的学号、姓名
select s1.stu_id, s1.stu_name, c1.course_name
from student_info s1
innerjoin score_info s2
on s1.stu_id = s2.stu_id
innerjoin course_info c1
on s2.course_id = c1.course_id
innerjoin teacher_info t1
on c1.tea_id = t1.tea_id
where t1.tea_name ='王子文';
查询学过“王子文”老师所讲授的任意一门课程的学生的学号、姓名
select s0.stu_id,
s0.stu_name
from student_info s0
where s0.stu_id in(select s1.stu_id
from score_info s1
where s1.course_id
in(select c1.course_id
from course_info c1
innerjoin teacher_info t1 on c1.tea_id = t1.tea_id
where t1.tea_name ='王子文'groupby c1.course_id));
查询没学过"王子文"老师讲授的任一门课程的学生姓名
select s0.stu_id,
s0.stu_name
from student_info s0
where s0.stu_id notin(select s1.stu_id
from score_info s1
where s1.course_id
in(select c1.course_id
from course_info c1
innerjoin teacher_info t1 on c1.tea_id = t1.tea_id
where t1.tea_name ='王子文'groupby c1.course_id));
查询至少有一门课与学号为“001”的学生所学课程相同的学生的学号和姓名
select s2.stu_id, s2.stu_name
from student_info s2
innerjoin score_info s3 on s2.stu_id = s3.stu_id
where s3.course_id in(select s1.course_id
from score_info s1
where s1.stu_id ='001'groupby s1.course_id)and s2.stu_id <>'001';
按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select s1.stu_id, s1.stu_name, c1.course_name, t1.avg_score as avg_score
from student_info s1
leftjoin score_info s2
on s1.stu_id = s2.stu_id
leftjoin course_info c1 on s2.course_id = c1.course_id
leftjoin(select stu_id,avg(score) avg_score from score_info groupby stu_id) t1
on s1.stu_id = t1.stu_id
orderby t1.avg_score desc;
presto引擎查询遇到的问题记录
Query failed
[2024-04-0515:04:57][1] Query failed (#20240405_070456_00065_j56k4): line 1:1: Schema must be specified when session schema is not set