SQL查询45道练习题
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
或者
select degree from score where degree in (85,86,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 stude