注意:Sno、Cno是主键
一、单表查询联系作业
1.1、查询选课表中全部数据
select * from SC
1.2、查询计算机系学生的学号、姓名和所在系部
select Sno, Sname, Sdept from Student
1.3、查询数学系年龄在18~22之间性别为“男”的学生的姓名和年龄
select Sname, Sage
from Student
where Sdept='数学' and Ssex='男' and Sage BETWEEN 18 AND 22
1.4、查询C01号课程的最低分
select min(Grade) from SC where Cno='C01'
1.5、统计每个系的学生人数
select Sdept, count(*) from Student group by Sdept
1.6、查询在计算机学院的女学生
select * from Student where Ssex='女' and Sdept='计算机'
1.7、查询学生表有多少行记录
select count(*) from Student
二、多表查询练习作业
2.1、查询选修了“C02”号课程的学生的姓名和所在系
select Sname, Sdept from Student, SC where Student.Sno=SC.Sno and SC.Cno='C02'
2.2、查询成绩在80分以上的学生的姓名、课程号和成绩,并按成绩降序排序
select Sname, Smajor, Grade
from Student, SC
where Student.Sno=SC.Sno and Grade>80 order by Grade DESC
2.3、查询哪些学生没有选课,要求列出学号、姓名和所在系
select Sno, Sname, Sdept from Student, SC where Sno NOT IN(
select Sno from SC
)
2.4、用子查询实现如下查询
2.4.1、查询选修了“C01”号课程的学生姓名和所在系
select Sname, Sdept from Student, SC where Sno IN(
select Sno from SC where Cno='C01'
)
2.4.2、查询数学系成绩80分以上的学生的学号、姓名、课程号和成绩
select Sno, Sname, Cno, Grade from Student, SC where Sdept='数学' and Sno IN(
select Sno from SC where Grade>80
)
2.4.3、查询计算机系考试成绩最高的学生的姓名
select Sname from Student, SC where Sdept='计算机' and Sno IN(
select Sno from SC where Grade IN(
select Max(grade) from SC
)
)