目录
3.查询各科成绩最高和最低的分, 以如下的形式显示:课程号,最高分,最低分
10.查询每门课程的平均成绩,结果按平均成绩升序排序,平均成绩相同时,按课程号降序排列
题目来源:知乎猴子 常见的SQL面试题:经典50题
一、简单查询
1. 查询姓“猴”的学生名单
select *
from student
where sname like "猴%"
2.查询姓“孟”老师的个数
select count(*)
from teacher
where tname like "孟%"
二、汇总分析
1.查询课程编号为“0002”的总成绩
select sum(score)
from score
where cid="0002"
2.查询选了课程的学生人数
select count(distinct sid)
from score
where cid is not null and score is not null
3.查询各科成绩最高和最低的分, 以如下的形式显示:课程号,最高分,最低分
select cid, max(score),min(score)
from score
group by cid
4.查询每门课程被选修的学生数
select cid,count(distinct sid)
from score
group by cid
5.查询男生、女生人数
select ssex,count(sid)
from student
group by ssex
6.查询平均成绩大于60分学生的学号和平均成绩
select sid, avg(score) as average
from score
group by sid
having average>60
7.查询至少选修两门课程的学生学号
select sid,count(distinct cid)
from score
group by sid
having count(distinct cid)>=2
8.查询同名同姓学生名单并统计同名人数
select sname, count(sid)
from student
group by sname
having count(sid)>=2
9.查询不及格的课程并按课程号从大到小排列
select distinct cid
from score
where score<60
order by cid desc