学斋随笔,初九潜龙勿用,-31关于查询的部分练习节选
1、 查询Student表中的所有记录的Sname、Ssex和Class列。
select sname,ssex,class from student
2、 查询教师所有的单位即不重复的Depart列。
select distinct depart from teacher
3、 查询Student表的所有记录。
select * from student
4、 查询Score表中成绩在60到80之间的所有记录。
select degree from score where Degree between 60 and 80
select degree from score where Degree>60 and degree<80
5、 查询Score表中成绩为85,86或88的记录。
select degree from score where degree=85 or degree=86 or degree=88
6、 查询Student表中“95031”班或性别为“女”的同学记录。
select * from student where class="95031" or ssex='女'
7、 以Class降序查询Student表的所有记录。
select * from student order by class desc
8、 以Cno升序、Degree降序查询Score表的所有记录。
select * from score order by cno asc,degree desc
9、 查询“95031”班的学生人数。
select count(*) from student where class="95031"
10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
排序查询:select * from score order by degree desc limit 0,1
子查询:select sno,cno from score where degree=(select max(degree) from score)
11、 查询每门课的平均成绩。
select cno,avg(degree) from score group by cno
12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
select avg(degree) from score where cno=( select cno from score group by cno having count(cno)>4 and cno like '3%')
select avg(degree) from score where cno like '3%' and cno in(select cno from score group by cno having count(cno)>4)
select avg(degree) from score group by cno having count(cno)>4 and cno like '3%'
13、查询分数大于70,小于90的Sno列。
select sno from score where degree>70 and degree<90;
14、查询所有学生的Sname、Cno和Degree列。
select student.Sname,score.Cno,score.Degree from student,score where student.Sno = score.Sno;
15、查询所有学生的Sno、Cname和Degree列。
select course.Cname,score.sno,score.Degree from course,score where course.cno = score.cno;
16、查询所有学生的Sname、Cname和Degree列。
Select student.Sname,course.Cname,score.Degree from student,course,score where score.Cno=course.Cno and score.Sno=student.sno
17、查询“95033”班学生的平均分。
select avg(degree) from score where sno in (select sno from student where class="95033");
18、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
select * from student where sno in(select sno from score where degree > (select degree from score where sno=109 and cno='3-105') and cno='3-105');
19、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
select student.sname,score.sno,score.cno,score.degree from student,score where student.sno=score.sno and score.sno in(select sno from score group by sno having count(cno)>1) and score.degree not in(select max(degree) from score)
20、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
select * from student where sno in(select sno from score where degree > (select degree from score where sno=109 and cno='3-105') and cno='3-105');
21、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
select sno,sname,sbirthday from student where year(sbirthday)= (select year(sbirthday) from student where sno=108);
22、查询“张旭“教师任课的学生成绩(姓名)。
select degree from score where cno= (select cno from course where tno= (select tno from teacher where tname='张旭'));
select teacher.tname, course.cname,score.cno from teacher,course,score where teacher.tno=course.tno and course.cno=score.cno and score.cno= (select cno from score group by cno having count(cno)>5)
23、查询95033班和95031班全体学生的记录。
select distinct student.*,score.* from student,score where student.sno=score.sno
24、查询成绩表中存在有85分以上成绩的课程Cno.
select distinct cno from score where degree>85
25、查询出“计算机系“教师所教课程的成绩表。
select * from score where cno in(select cno from course where tno in(select tno from teacher where depart='计算机系'));
26、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的 Cno、Sno和Degree.
select cno,sno,degree from score a where cno='3-105' and degree>all(select degree from score b where cno='3-245' and b.sno=a.sno) order by degree desc
27、查询所有成绩比3-105课程平均成绩低的同学的成绩表。
select * from score a where degree<(select avg(degree) from score b where b.cno =a.cno)
28、查询所有任课教师的Tname和Depart.
select tname,depart from teacher where tno in(select tno from course where cno in(select cno from score))
29、查询所有未讲课的教师的Tname和Depart.
select tname,depart from teacher where tno not in (select tno from course where cno in (select distinct cno from score))
30、查询至少有2名男生的班号。
select distinct class from student a where (select count(ssex) from student b where ssex ='男' and a.class=b.class )>1
31、查询Student表中不姓“王”的同学记录。
select * from student where sname not like '王%'
32、查询Student表中每个学生的姓名和年龄。
select student.sname,(2016-year(sbirthday)) as sage from student where true
33、查询Student表中最大和最小的Sbirthday日期值。
select min(sbirthday),max(sbirthday) from student
34、以班号和年龄从大到小的顺序查询Student表中的全部记录。
select * from student order by class desc,sbirthday asc
35、查询“男”教师及其所上的课程。
select teacher.Tname,course.Cname from teacher,course where course.Tno=teacher.Tno and tsex='男'
36、查询最高分同学的Sno、Cno和Degree列。
select sno,cno,degree from score where degree=(select max(degree) from score)
37、查询和“李军”同性别的所有同学的Sname.
select sname from student where ssex=(select ssex from student where sname='李军')
38、查询和“李军”同性别并同班的同学Sname.
select sname from student a where ssex=(select ssex from student b where sname='李军' and a.class=b.class)
39、查询所有选修“计算机导论”课程的“男”同学的成绩表。
select student.sno,student.sname,score.degree from student,course,score where student.ssex='男' and course.cname='计算机导论' and student.sno=score.sno and course.cno=score.cno

浙公网安备 33010602011771号