--1.查询"01"课程比"02"课程成绩高的学生的信息及课程分数select s3.*,s1.score,s2.score
from score s1 join score s2 join student s3
on s1.student_id=s2.student_id and s2.student_id = s3.student_id
where s1.course_id=1and s2.course_id=2and s1.score>s2.score;--2.查询"01"课程比"02"课程成绩低的学生的信息及课程分数select s3.*,s1.score,s2.score
from score s1 join score s2 join student s3
on s1.student_id=s2.student_id and s2.student_id = s3.student_id
where s1.course_id=1and s2.course_id=2and s1.score<s2.score;--3.查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩with t1 as(selectround(avg(s1.score)) r1,s1.student_id
from score s1
groupby s1.student_id)select t1.student_id,t2.student_name,t1.r1
from t1 join student t2
on t1.student_id=t2.student_id
where t1.r1>=60;--4.查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩with t1 as(selectround(avg(s1.score)) r1,s1.student_id
from score s1
groupby s1.student_id)select t1.student_id,t2.student_name,t1.r1
from student t2 leftjoin t1
on t1.student_id=t2.student_id
where t1.r1<60or t1.r1 isnull;--5.查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩with t1 as(select s.student_id,count(c.course_id),sum(s.score)from score s join course c
on s.course_id=c.course_id
groupby s.student_id)select st.student_name,t1.*from student st join t1
on st.student_id=t1.student_id;--6.查询"李"姓老师的数量selectcount(teacher_id)from teacher
where teacher_name like'李%';--7.查询学过"张三"老师授课的同学的信息with t1 as(select teacher_id from teacher where teacher_name='张三'),
t2 as(select c.course_id cid from course c join t1 on c.teacher_id=t1.teacher_id),
t3 as(select s.student_id sid from score s join t2 on s.course_id=t2.cid)select st.*from student st join t3 on st.student_id=t3.sid;--8.查询没学过"张三"老师授课的同学的信息selectdistinct s1.*from student s1 leftjoin score s3 on s1.student_id=s3.student_id
wherenotexists(select s2.*from score s1 join course c join teacher t1 join student s2 join(select teacher_id from teacher where teacher_name='张三') a
on s1.course_id = c.course_id and t1.teacher_id = c.teacher_id and s1.student_id = s2.student_id and a.teacher_id=t1.teacher_id
--9.查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息selectdistinct a.*from(select s1.*,count(s2.course_id)over(partitionby s2.student_id)as num
from student s1 join score s2
on s1.student_id=s2.student_id
where s2.course_id!=3) a
where a.num=2;--10.查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息with a as(select student_id from score where course_id=1),
b as(select s.student_id sid from score s
join a on s.student_id=a.student_id
where s.student_id notin(select student_id from score where course_id=2))selectdistinct s.*from student s join b
on s.student_id=b.sid;--11.查询没有学全所有课程的同学的信息select st.*from student st
where st.student_id notin(select a.sid tid from(select s.student_id sid,count(1) num from score s
groupby s.student_id) a
join(selectcount(1) num from course) b
on a.num=b.num);--12.查询至少有一门课与学号为"01"的同学所学相同的同学的信息select st.*from student st
join(selectdistinct sc.student_id sid from score sc
where sc.student_id!=1and sc.course_id in(select s.course_id cid from score s
where s.student_id=1)) a
on st.student_id=a.sid;--13.查询和"01"号的同学学习的课程完全相同的其他同学的信息select st.*from student st
join(select a.sid cid from(select student_id sid,count(course_id) num from score
where student_id!=1groupby student_id) a
join(selectcount(s.course_id) cont from score s
where s.student_id=1) b
where a.num=b.cont) c
on st.student_id=c.cid;--14.查询没学过"张三"老师讲授的任一门课程的学生姓名select st.*from student st
where st.student_id notin(select s1.student_id from score s1
where s1.course_id=2);--15.查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩select*from(select s1.student_id,count(s1.score) cont,avg(s1.score) acont from score s1
where s1.score<60groupby s1.student_id) a
where a.cont>=2;--16.检索"01"课程分数小于60,按分数降序排列的学生信息select st.*,s.score from score s
join student st
on st.student_id=s.student_id
where s.course_id=1and s.score <60orderby s.score desc;--17.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩select s.score,avg(s.score)over(partitionby s.student_id) avgg
from score s
orderby avgg;--18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率select s.course_id,c.course_name,max(s.score),min(s.score),avg(s.score)from score s
join course c
on s.course_id=c.course_id
groupby s.course_id,c.course_name;--19.按各科成绩进行排序,并显示排名select s.course_id,st.student_name,s.score,
row_number()over(partitionby s.course_id orderby s.score desc)as ronum
from score s
join student st
on s.student_id=st.student_id;--20.查询学生的总成绩并进行排名select s.student_id,st.student_name,sum(s.score) ssum from score s
join student st
on st.student_id=s.student_id
groupby s.student_id,st.student_name
orderby ssum desc;--21.查询不同老师所教不同课程平均分从高到低显示select c.teacher_id,t.teacher_name,avg(s.score) avgg
from score s
join course c
join teacher t
on c.course_id=s.course_id and c.teacher_id=t.teacher_id
groupby s.course_id,c.teacher_id,t.teacher_name
orderby avgg desc;--22.查询所有课程的成绩第2名到第3名的学生信息及该课程成绩with t1 as(select s.student_id sid,s.score sce,
row_number()over(partitionby s.course_id orderby s.score desc)as rnum
from score s)select st.*,t1.sce,t1.rnum
from student st
join t1
on st.student_id=t1.sid
where t1.rnum in(2,3);--23.统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比--24.查询学生平均成绩及其名次with t1 as(select s.student_id sid,avg(s.score) avgg
from score s
groupby s.student_id)select t1.sid,st.student_name,t1.avgg
from student st join t1
on st.student_id=t1.sid
orderby t1.avgg desc;--25.查询各科成绩前三名的记录select a.*from(select s.course_id,s.student_id,st.student_name,s.score,
row_number()over(partitionby s.course_id orderby s.score)as rnum
from score s join student st
on s.student_id=st.student_id) a
where a.rnum<=3;--26.查询每门课程被选修的学生数selectcount(s.student_id)from score s
groupby s.course_id;--27.查询出只有两门课程的全部学生的学号和姓名with t1 as(select s.student_id sid,count(s.student_id) num from score s
groupby s.student_id)select st.student_id,st.student_name from student st
join t1
on st.student_id=t1.sid
where t1.num=2;--28.查询男生、女生人数 select s.student_sex,count(s.student_sex)from student s
groupby s.student_sex;--29.查询名字中含有"风"字的学生信息select*from student
where student_name like'%风%';--30.查询同名同性学生名单,并统计同名人数select student_name,count(*)from student
groupby student_name
havingcount(*)>1;--31.查询1990年出生的学生名单select*from student
whereyear(student_birth)=1990;--32.查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列select course_id,avg(score)as a
from score
groupby course_id
orderby a desc,course_id asc;--33.查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩with t1 as(select student_id sid,avg(score) avgg
from score
groupby student_id
havingavg(score)>=85)select t1.sid,st.student_name,t1.avgg
from t1
join student st
on t1.sid=st.student_id;--34.查询课程名称为"数学",且分数低于60的学生姓名和分数select st.student_name,s.score
from student st
join score s
on st.student_id=s.student_id
where s.course_id
in(select course_id from course where course_name='数学');--35.查询所有学生的课程及分数情况select st.student_name,c.course_name,s.score
from student st
join course c
join score s
on st.student_id=s.student_id and c.course_id=s.course_id;--36.查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数with t1 as(select student_id sid,course_id cid,score
from score
where score>70)select st.student_name,c.course_name,t1.score
from t1
join student st
join course c
on st.student_id=t1.sid and t1.cid=c.course_id;--37.查询课程不及格的学生with
t1 as(selectdistinct student_id
from score
where score <60)select s.*from student s join t1 on t1.student_id=s.student_id;--38.查询课程编号为01且课程成绩在80分以上的学生的学号和姓名select s.student_id,st.student_name
from student st
join score s
on s.student_id=st.student_id and s.course_id=1where s.score>=80;--39.求每门课程的学生人数select course_id,count(1)from score
groupby course_id;--40.查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩with t1 as(select teacher_id from teacher where teacher_name='张三'),
t2 as(select c.course_id cid from course c join t1 on c.teacher_id=t1.teacher_id),
t3 as(select s.student_id sid,s.score score from score s join t2 on s.course_id=t2.cid)select st.*,t3.score from student st
join t3
on st.student_id=t3.sid
orderby t3.score desclimit1;--41.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩select s1.student_id,s2.student_id,s1.course_id,s2.course_id,s1.score
from score s1 join score s2
on s1.student_id=s2.student_id
where s1.score=s2.score and s1.course_id!=s2.course_id;--42.查询每门课程成绩最好的前三名with t1 as(select*,row_number()over(partitionby course_id orderby score)as rnum from score)select*from t1
where t1.rnum<=3;--43.统计每门课程的学生选修人数(超过5人的课程才统计):--要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列with t1 as(select course_id,count(1) cont
from score
groupby course_id
orderby course_id)select*from t1
where t1.cont>5;--44.检索至少选修两门课程的学生学号with t1 as(select student_id,count(1) cont from score groupby student_id)select*from student st
join t1
on st.student_id=t1.student_id
where t1.cont>=2;--45.查询选修了全部课程的学生信息select st.*from student st
where st.student_id in(select a.sid tid from(select s.student_id sid,count(1) num from score s
groupby s.student_id) a
join(selectcount(1) num from course) b
on a.num=b.num);--46.查询各学生的年龄(周岁):-- 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一select*, cast(date_format(current_date(),'yyyy')asint)-cast(date_format(student_birth,'yyyy')asint)from student;--47、查询本周过生日的学生:-- 找到下周一-1即为本周最后一天,开始时间为当前天select*from student
where unix_timestamp(cast(concat_ws('-',date_format(current_date(),'yyyy'),date_format(student_birth,'MM'),date_format(student_birth,'dd'))asdate),'yyyy-MM-dd')between unix_timestamp(current_date())and unix_timestamp(date_sub(next_day(current_date(),'MON'),1),'yyyy-MM-dd');--48、查询下周过生日的学生:-- 找到下周的最后一天和最开始一天来算 next_day(current_date(),'SUNDAY'); -- 需要注意的是老外以星期天,开始需要+7select*from student
where unix_timestamp(cast(concat_ws('-',date_format(current_date(),'yyyy'),date_format(student_birth,'MM'),date_format(student_birth,'dd'))asdate),'yyyy-MM-dd')between unix_timestamp(next_day(current_date(),'MON'),'yyyy-MM-dd')and unix_timestamp(date_add(next_day(current_date(),'SUN'),7),'yyyy-MM-dd');--49、查询本月过生日的学生:select*from student
wheremonth(student_birth)=month(current_date());--50、查询12月份过生日的学生:select*from student
wheremonth(student_birth)=12;
一、创建数据库并使用数据库--创建数据库create database if not exists lyq;--使用数据库use lyq;二、创建所需要的表,并插入数据--创建表-- 课程表create table if not exists course(course_id int,course_name string,teacher_id int);-- 分数表create table if not exists score(student_id int,course_id int