创建四张表 分别存储 学生信息 课程信息 分数 讲师信息表 存储相应数据
student学生信息表
course课程信息表
scores分数表
teacher讲师表
目录
1、 查询Student表中的所有记录的Sname、Ssex和Class列。
6、 查询Student表中“95031”班或性别为“女”的同学记录。
8、 以Cno升序、Degree降序查询Score表的所有记录。
12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
16、查询所有学生的Sname、Cname和Degree列。
19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
31、查询所有教师和同学的name、sex和birthday.
32、查询所有“女”教师和“女”同学的name、sex和birthday.
39、查询Student表中最大和最小的Sbirthday日期值。
40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
1、 查询Student表中的所有记录的Sname、Ssex和Class列。
select sname.ssex.class from student;
查询结果:
2、 查询教师所有的单位即不重复的Depart列。
select distinct Depart from Teacher;
![](https://i-blog.csdnimg.cn/blog_migrate/2ce8d79072b6a82a9029009b467e6502.png)
3、 查询Student表的所有记录。
select *from student;
![](https://i-blog.csdnimg.cn/blog_migrate/afadfad60b50a95e566dd758545c6e81.png)
4、 查询Score表中成绩在60到80之间的所有记录。
select * from Score where degree between 60 and 80;
![](https://i-blog.csdnimg.cn/blog_migrate/087afd2ec775e6f8d21c4d2c4f441467.png)
5、 查询Score表中成绩为85,86或88的记录。
select *from score where degree in(85,86,88);
![](https://i-blog.csdnimg.cn/blog_migrate/22d5899c8894b279b2903059b4330623.png)
6、 查询Student表中“95031”班或性别为“女”的同学记录。
select *from student where class=95031 or Ssex='女';
![](https://i-blog.csdnimg.cn/blog_migrate/09427fb7cf2d61551fb37e52ce414b2a.png)
7、 以Class降序查询Student表的所有记录。
select *from student order by class desc;
![](https://i-blog.csdnimg.cn/blog_migrate/21ce7daa4e125c7d5befe9d0c93c6092.png)
8、 以Cno升序、Degree降序查询Score表的所有记录。
select *from score order by cno,degree desc;
103 3-105 92.0
107 3-105 91.0
105 3-105 88.0
108 3-105 78.0
109 3-105 76.0
101 3-105 64.0
103 3-245 86.0
105 3-245 75.0
109 3-245 68.0
107 6-106 79.0
101 6-166 85.0
108 6-166 81.0
9、 查询“95031”班的学生人数。
select count(class) from student where class=95031;
![](https://i-blog.csdnimg.cn/blog_migrate/112857a7a949b061f537126dc810ab7a.png)
10、查询Score表中的最高分的学生学号和课程号。
select sno,cno from score where degree=(select max(degree)from score);
![](https://i-blog.csdnimg.cn/blog_migrate/0b41513bc93c3280281acb6f341c2b73.png)
11、查询‘3-105’号课程的平均分。
select avg(degree)from score where cno='3-105' group by cno;
12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
select avg(degree) from score
where cno = (select cno from score group by cno having count(cno)>=5 ) and cno like '3%';
此处有踩坑,踩坑原因 :字符编码问题,神奇的空格
运行结果
13、查询最低分大于70,最高分小于90的Sno列。
思路:这里需要注意sql语句的执行顺序,分组函数不能再where的子句中,所以不可以直接用where,需要用having分组之后筛选。
select sno from score group by sno having min(degree)>70 ang max(degree)<90;
![](https://i-blog.csdnimg.cn/blog_migrate/31885681aab6677b0d3869769fa42ea9.png)
14、查询所有学生的Sname、Cno和Degree列。
分析:sname在student表,cno与degree在score表。找到两表关联,用sno字段连接两表;
select stu.sname,s.cno,s.degree from student stu,score s where stu.sno=s.sno;
![](https://i-blog.csdnimg.cn/blog_migrate/a5d934910c5d6279557ffa83652f3535.png)
15、查询所有学生的Sno、Cname和Degree列。
分析:cname在course表,sno与degree在score表,找到两表关联,用cno字段连接两表;
select sno,cname,degree from course c,score s where c.cno=s.cno;
![](https://i-blog.csdnimg.cn/blog_migrate/62a702876d2a514e1494c26c8ee96579.png)
16、查询所有学生的Sname、Cname和Degree列。
分析sname在student表,cname在course表,degree在score表
三表连接student,course,score
先用student连接course,再连接score
select stu.sname,c.cname,s.degree,from student stu,course c,score s where stu.sno=s.sno and c.cno=s.cno;
17、查询“95033”班所选课程的平均分。
分析:student中的class为95033
select avg(degree)from student stu,score s where stu.sno=s.sno and stu.class=95033;
18、假设使用如下命令建立了一个grade表:
create table grade(low int(3),upp int(3),rank char(1));
insert into grade values(90,100,'A');
insert into grade values(80,89,'B');
insert into grade values(70,79,'C');
insert into grade values(60,69,'D');
insert into grade values(0,59,'E');
commit;
现查询所有同学的Sno、Cno和rank列。
select s.sno,s.cno,g.rank from score s,grade g where s.degree between g.low and g.upp;