10题: 查询Score表中的最高分的学生学号和课程号。
select * from score // 需要用到score表中的内容
select sno,cno from score // 需要用到score表中sno,cno 两列的内容
select max(degree) from score // 查询最高分数
select sno,cno from score where degree=(select max(degree) from score) // 将最高分的限制条件带入式二得出结果
12题:查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
select avg(degree) from score where cno=() //求出score表中课程号为()的课程的平均分
select cno,count(cno) from score group by cno //求出每门课程选修的学生有多少
select cno from score group by cno having count(cno)>=5 //求出选修人数超过5的课程的代号
select avg(degree) from score where cno=(select cno from score group by cno having count(cno)>=5 and cno like‘3%‘) // 将限制条件至少5名学生选修跟以3开头两个条件带入首式中即可得出结果
16题:查询所有学生的Sname、Cname和Degree列。
1.sname 需要用到 student 表,cname 需要用到 course 表,degree 需要用到score 表
2.需要将三个表建立起联系 student.sno=score.sno course.cno=score.cno
select sname,cname,degree from student,course,score where student.sno=score.sno and course.cno=score.cno //将联系条件带入查询可得结果
17题:查询“95033”班学生的平均分。
1.需要用到score表的内容
select * from score
2.需要用到“95033”班学生的成绩
由于score表中没有class内容,只有学生学号sno,所以就需要到student表中进行调取,查看“95033”班的学生学号是多少
select sno from student where class=‘95033‘
3.然后再求每个学生每门课的成绩
select sno,cno,degree from score where sno in (select sno from student where class=‘95033‘)
4.“95033”班的每个学生的平均分
select sno,avg(degree) from score group by sno having sno in (select sno from student where class=‘95033‘)
5.“95033”班的学生每门课的平均分
select cno,avg(degree) from score group by cno having sno in (select sno from student where class=‘95033‘)
6.“95033”班的学生成绩的平均分
select avg(degree) from score where sno in (select sno from student where class=‘95033‘)
19题:查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
select sno,degree from score where cno=‘3-105‘ //查询选修3-105课程的学生的学号和成绩
select degree from score where sno=‘109‘ //查询109号同学的成绩
select * from score where cno=‘3-105‘ and degree>(select degree from score where sno=‘109‘) //将限定条件带入查询语句可得结果
20题:查询score中选学多门课程的同学中分数为非最高分成绩的记录。
1.选学多门课程的同学学号
select sno from score group by sno having count(*)>1
2.每门课的最高分
select max(degree) from score group by cno
3.求结果时,科目跟分数是相关联的,所以需要用到相关子查询
select * from score a where degree <(select max(degree) from score b group by cno having a.cno=b.cno) and sno in (select sno from score group by sno having count(*)>1)
22题:查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
1.需要用到student表的内容
select * from student
2.要查询的内容是sno,sname,sbirthday
select sno,sname,sbirthday from student
3.需要跟学号为108的同学同年出生,因此需要用到108号学生的生日中的年
select year(sbirthday) from student where sno=‘108‘
4.将限定条件带入查询语句
select sno,sname,sbirthday from student where year(sbirthday) in (select year(sbirthday) from student where sno=‘108‘)
28题:查询“计算机系”与“电子工程系”不同职称的教师的Tname和Prof。
1.需要用到teacher表
select * from teacher
2.需要用到系别为“计算机系”的教师的职称
select prof from teacher where depart=‘计算机系‘
3.需要用到系别为“电子工程系”的教师的职称
select prof from teacher where depart=‘电子工程系‘
4.职称相同的教师职称
select prof from teacher where depart=‘计算机系‘
and
prof in(select prof from teacher where depart=‘电子工程系‘)
5.所需的教师必须是“计算机系”与“电子工程系”
depart in(‘计算机系‘,‘电子工程系‘)
6.将所有条件带入查询语句
select tname,prof from teacher where
prof not in
(
select prof from teacher where depart=‘计算机系‘
and
prof in (select prof from teacher where depart=‘电子工程系‘)
)
and
depart in (‘计算机系‘,‘电子工程系‘)
29题:查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
1.需要用到score表
select * from score
2.选修“3-105”的学生有哪些
select * from score where cno=‘3-105‘
3.选修“3-245”的同学的最高成绩是多少
select max(degree) from score where cno=‘3-245‘
4.从高到低排序需用到order by degree desc
5.将所有条件带入查询语句
select cno,sno,degree from score where
degree>(select max(degree) from score where cno=‘3-245‘)
and
cno=‘3-105‘
order by degree desc
老师给出的另两种求解方式:
select * from score where cno=‘3-105‘ and degree>(select min(degree) from score where cno = ‘3-245‘) //只要是大于选修编号为“3-245”的课程的学生的最低得分就可以算作结果
select cno,sno,degree from score where cno=‘3-105‘ and degree>any(select degree from score where cno = ‘3-245‘) //只要是大于任意一个选修编号为“3-245”的课程的学生的分数就可以算作结果
32题:查询所有“女”教师和“女”同学的name、sex和birthday.
1.需要用到student表的sname,ssex,sbirthday并要改名称为name,sex,birthday,条件必须是“女”的
select sname as name,ssex as sex,sbirthday as birthday from student where ssex=‘女‘
2.需要用到teacher表的tname,tsex,tbirthday并要改名称为name,sex,birthday
select tname as name,tsex as sex,tbirthday as birthday from teacher where tsex=‘女‘
3.需要用到联合查询建立对行的扩展 用到union
select sname as name,ssex as sex,sbirthday as birthday from student where ssex=‘女‘
union
select tname as name,tsex as sex,tbirthday as birthday from teacher where tsex=‘女‘
33题:查询成绩比该课程平均成绩低的同学的成绩表。
1.需要用到成绩表
select * from score
2.需要用到每门课的平均成绩
select avg(degree) from score group by cno
3.相同课程中比该课程平均成绩低的同学
select * from score a where degree<(select avg(degree) from score b group by cno having a.cno=b.cno)
//首先明确最后提取的内容是外层表中的,在提取过程中,系统需要将内层表与外层表的内容进行比较,所以要分别命名外层表跟内层表为a,b以作区别,而系统在调用时是每个都需要调用,而不会是一一对应的调用,所以为了防止在调用过程中,发生数据的重复,要设定只有当两个表中的对应项相等时,该条数据才被提取
40题:以班号和年龄从大到小的顺序查询Student表中的全部记录。
1.班号从大到小 用到 order by class desc
2.年龄从大到小 可以用生日进行比较用到 sbirthday order by (now()-sbirthday) desc // desc 降序排列
3.查询结果
select * from student order by class desc , (now()-sbirthday ) desc // 升序排列是 asc 为系统默认,可省略