针对数据库单表查询的三张表进行查询:
//查询未选课的学生情况包含学生学号、姓名和所在系;
select Student.Sno 学号,Sname 姓名,Sdept 系,Cno 课程号
from Student
left outer join SC on(Student.Sno=SC.Sno)
where cno is null;
//查询已选课学生的选课情况包含学生学号、姓名、所选课程、成绩;
select Student.Sno 学号,Sname 姓名,Cname 课程号,Grade 成绩
from Student ,SC,Course
where (Student.Sno=SC.Sno) and (SC.Cno=Course.Cno);
//查询所有学生的选课情况包含学生学号、姓名、课程号、课程名、成绩;(包含选课和未选课所有学生);
select Student.Sno 学号,Sname 姓名,Cname 课程号,Grade 成绩
from Student
left outer join SC on(Student.Sno=SC.Sno)
left outer join Course on(SC.Cno=Course.Cno);
//查询没有学生选的课程信息包含课程号、课程名
select Course.Cno 课程号,Cname 课程名,Sno 学号
from SC
right outer join Course on(SC.Cno=Course.Cno)
where Sno is null;
//查询已被选择课程的课程信息包含课程号、课程名、学生学号、姓名、成绩;
select Course.Cno 课程号,Cname 课程名,Student.Sno 学号 ,Sname 学生姓名,Grade 成绩
from SC,Student,Course
where (SC.Cno=Course.Cno) and (Student.Sno=SC.Sno);
//查询所有课程的选课情况包含课程号、课程名、学生学号、姓名、成绩;(包含没有学生选课的课程)
select Course.Cno 课程号,Cname 课程名,Student.Sno 学号 ,Sname 学生姓名,Grade 成绩
from Student
right outer join SC on(Student.Sno=SC.Sno)
right outer join Course on(SC.Cno=Course.Cno);
//查询每个学生选课的总学分,并按总学分的降序排列;
select sum(Ccredit) 总学分,Sno
from SC,Course where SC.Cno=Course.Cno
group by Sno
order by(sum(Ccredit))desc;
//查询选修了数据库课程且成绩在60分以下的学生的学号、姓名、课程名及成绩;
select Student.Sno 学号,Sname 学生姓名,Sdept 系,Course.Cno 课程号,Grade 成绩
from Student,SC,Course
where (SC.Cno=Course.Cno) and (SC.Sno=Student.Sno) and Grade < 60 and Cname='数据库';