26、查询每门课程被选修的学生数:
select course_id,count(stu_id)
from
t_score
group by course_id
;
运行结果:
1 6
2 6
3 6
27、查询出只有两门课程的全部学生的学号和姓名:
select a.*,count(a.stu_id)
from
t_stu_info a join t_score b on a.stu_id=b.stu_id
group by a.stu_id,a.stu_name,a.birthday,a.gender
having count(a.stu_id)=2
;
运行结果:
5 周梅 1991-12-01 女 2
6 吴兰 1992-03-01 女 2
7 郑竹 1989-07-01 女 2
28、查询男生、女生人数:
select gender,count(gender)
from
t_stu_info
group by gender
;
运行结果:
女 4
男 4
29、查询名字中含有"风"字的学生信息:
select *
from
t_stu_info
where stu_name like '%风%'
;
运行结果:
3 孙风 1990-05-20 男
//从该题开始向表中添加了两条 同名 数据
30、查询同名同性学生名单,并统计同名人数:
select stu_name,gender,count(stu_name)
from
t_stu_info
group by stu_name,gender
having count(stu_name)>1
;
运行结果:
王菊 女 2
郑竹 女 2
31、查询1990年出生的学生名单:
select t_stu_info.*
from
t_stu_info
where substring(birthday,0,4)='1990'
;
运行结果:
1 赵雷 1990-01-01 男
2 钱电 1990-12-21 男
3 孙风 1990-05-20 男
4 李云 1990-08-06 男
8 王菊 1990-01-20 女
10 王菊 1990-02-09 男
32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列:
select course_id,
round(avg(score),2) as avg
from
t_score
group by course_id
order by avg desc,course_id asc
;
运行结果:
2 72.67
3 68.5
1 64.5
33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩:
select *
from
(
select a.*,
round(avg(b.score),2) as avg
from
t_stu_info a join t_score b on a.stu_id=b.stu_id
group by a.stu_id,a.stu_name,a.birthday,a.gender
) a
where a.avg>85
;
运行结果:
1 赵雷 1990-01-01 男 89.67
7 郑竹 1989-07-01 女 93.5
34、查询课程名称为"数学",且分数低于60的学生姓名和分数:
select
a.*,
b.score,
c.course
from
t_stu_info a left join t_score b on a.stu_id=b.stu_id
left join t_course c on b.course_id=c.course_id
where (c.course='数学' and b.score<60) or c.course is null
;
运行结果:
4 李云 1990-08-06 男 30 数学
8 王菊 1990-01-20 女 NULL NULL
9 郑竹 1989-07-02 男 NULL NULL
10 王菊 1990-02-09 男 NULL NULL
35、查询所有学生的课程及分数情况:
select a.*,b.score,c.course
from
t_stu_info a left join t_score b on a.stu_id=b.stu_id
left join t_course c on b.course_id=c.course_id
;
运行结果:
1 赵雷 1990-01-01 男 80 语文
1 赵雷 1990-01-01 男 90 数学
1 赵雷 1990-01-01 男 99 英语
2 钱电 1990-12-21 男 70 语文
2 钱电 1990-12-21 男 60 数学
2 钱电 1990-12-21 男 80 英语
3 孙风 1990-05-20 男 80 语文
3 孙风 1990-05-20 男 80 数学
3 孙风 1990-05-20 男 80 英语
4 李云 1990-08-06 男 50 语文
4 李云 1990-08-06 男 30 数学
4 李云 1990-08-06 男 20 英语
5 周梅 1991-12-01 女 76 语文
5 周梅 1991-12-01 女 87 数学
6 吴兰 1992-03-01 女 31 语文
6 吴兰 1992-03-01 女 34 英语
7 郑竹 1989-07-01 女 89 数学
7 郑竹 1989-07-01 女 98 英语
8 王菊 1990-01-20 女 NULL NULL
9 郑竹 1989-07-02 男 NULL NULL
10 王菊 1990-02-09 男 NULL NULL
36、查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数:
select c.stu_id,c.stu_name,e.course,d.score
from
(
select a.stu_id,a.stu_name
from
t_stu_info a left join t_score b on a.stu_id=b.stu_id
group by a.stu_id,stu_name
having sum(case when b.score >=70 then 1 else 0 end)> 0
) c join t_score d on c.stu_id=d.stu_id
join t_course e on d.course_id=e.course_id
;
运行结果:
1 赵雷 语文 80
1 赵雷 数学 90
1 赵雷 英语 99
2 钱电 语文 70
2 钱电 数学 60
2 钱电 英语 80
3 孙风 语文 80
3 孙风 数学 80
3 孙风 英语 80
5 周梅 语文 76
5 周梅 数学 87
7 郑竹 数学 89
7 郑竹 英语 98
37、查询课程不及格的学生:
select a.stu_id,a.stu_name
from
t_stu_info a left join t_score b on a.stu_id=b.stu_id
group by a.stu_id,a.stu_name
having sum(case when b.score<60 then 1 else 0 end)>0
;
运行结果:
4 李云
6 吴兰
38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名:
select a.*
from
t_stu_info a join t_score b on a.stu_id=b.stu_id and b.course_id='1' and b.score>=80
;
运行结果:
1 赵雷 1990-01-01 男
3 孙风 1990-05-20 男
39、求每门课程的学生人数:
select a.course_id,b.course,count(a.course_id)
from
t_score a join t_course b on a.course_id=b.course_id
group by a.course_id,b.course
;
运行结果:
1 语文 6
2 数学 6
3 英语 6
40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩:
select b.*,a.score
from
(
select b.stu_id,b.score,
first_value(b.score) over (partition by b.course_id order by b.score desc rows between unbounded preceding and unbounded following)
from
t_score b
join t_course c on b.course_id=c.course_id
join t_teach d on c.teach_id=d.teach_id
where d.teach_name='张三'
limit 1
) a join t_stu_info b on a.stu_id=b.stu_id
;
运行结果:
1 赵雷 1990-01-01 男 90
41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩:
select distinct a.stu_id,b.stu_id,a.course_id,b.course_id,b.score
from
t_score a join t_score b on a.score=b.score
where a.stu_id<>b.stu_id and a.course_id<>b.course_id
运行结果:
1 2 1 3 80
1 3 1 2 80
1 3 1 3 80
2 1 3 1 80
2 3 3 1 80
2 3 3 2 80
3 1 2 1 80
3 1 3 1 80
3 2 1 3 80
3 2 2 3 80
42、查询每门课程成绩最好的前三名:
select *
from
(
select course_id,score,
row_number() over(partition by course_id order by score desc) as rn
from
t_score
) a
where a.rn<4
;
运行结果:
1 80 1
1 80 2
1 76 3
2 90 1
2 89 2
2 87 3
3 99 1
3 98 2
3 80 3
43、统计每门课程的学生选修人数(超过5人的课程才统计):
– 要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select course_id,
count(1) as total
from
t_score
group by course_id
having count(1) > 5
order by course_id asc,total desc
;
运行结果:
1 6
2 6
3 6
44、检索至少选修两门课程的学生学号:
select stu_id,
count(stu_id) as total
from
t_score
group by stu_id
having total>1
;
运行结果:
1 3
2 3
3 3
4 3
5 2
6 2
7 2
45、查询选修了全部课程的学生信息:
select stu.stu_id,stu.stu_name
from t_stu_info stu
join t_course cs
left join t_score sc on sc.stu_id = stu.stu_id and cs.course_id = sc.course_id
group by stu.stu_id,stu.stu_name
having sum(case when sc.score is null then 1 else 0 end)=0
;
运行结果:
1 赵雷
2 钱电
3 孙风
4 李云
46、查询各学生的年龄(周岁):
– 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
select a.*,
(case when month(a.birthday)>=month(current_date) and dayofmonth(a.birthday)>dayofmonth(current_date)
then
year(current_date)-year(a.birthday)-1
else
year(current_date)-year(a.birthday)
end)
from
t_stu_info a
;
运行结果:
1 赵雷 1990-01-01 男 28
2 钱电 1990-12-21 男 28
3 孙风 1990-05-20 男 28
4 李云 1990-08-06 男 28
5 周梅 1991-12-01 女 27
6 吴兰 1992-03-01 女 26
7 郑竹 1989-07-01 女 29
8 王菊 1990-01-20 女 28
9 郑竹 1989-07-02 男 29
10 王菊 1990-02-09 男 28.
47、查询本周过生日的学生:
select a.*,
weekofyear(a.birthday),
weekofyear(current_date)
from
t_stu_info a
where (weekofyear(a.birthday)=weekofyear(current_date))
;
运行结果:
当前时间2018-12-27 本周没过生日的....
48、查询下周过生日的学生:
select a.*,
weekofyear(a.birthday),
weekofyear(current_date)
from
t_stu_info a
where
(case when weekofyear(current_date)=52 then 1 else weekofyear(current_date)+1 end)= weekofyear(a.birthday)
;
运行结果:
1 赵雷 1990-01-01 男 1 52
49、查询本月过生日的学生:
select a.*,
month(a.birthday),
month(current_date)
from
t_stu_info a
where (month(a.birthday)= month(current_date))
;
运行结果:
2 钱电 1990-12-21 男 12 12
5 周梅 1991-12-01 女 12 12
50、查询12月份过生日的学生:
select a.*,
month(a.birthday)
from
t_stu_info a
where (month(a.birthday)= 12)
;
运行结果:
2 钱电 1990-12-21 男 12 12
5 周梅 1991-12-01 女 12 12