一、实验目的
牢记SELECT 语句的基本语法格式;掌握利用SQL语句进行多表内/外连接查询和嵌套查询的方法;掌握GROUP BY子句、HAVING子句、ORDER BY子句的用法;掌握(NOT)IN、(NOT)EXISTS等谓词的用法;掌握SUM、AVG、COUNT、MAX、MIN等集合函数的用法。
二、实验内容及要求
在建立的三张表(student、course、sc)的基础上,
Sc:
Student:
Course:
(1)查询计算机系学生的信息;
select Sno,Sname,Ssex,Sage
from Student
where Sdept='CS'
(2)查询全体学生的人数;
select COUNT(*)
from Student
(3)查询全体男生人数和女生人数;
select COUNT(*)
from Student
where Ssex='男'
select COUNT(*)
from Student
where Ssex='女'
(4)查询每个系中的男生人数并按人数的降序排列;
select Sdept,COUNT(Ssex)
from Student
group by Sdept,Ssex
having Ssex='男'
order by COUNT(Ssex) desc
(5)查询名字中带“李”的学生信息;
select *
from Student
where Sname LIKE '%李%'
(6)求被选修的各门课程的平均成绩和选修该课程的人数;
select Cno,COUNT(Sno),AVG(Greade)
from sc
Group by Cno
(7)查找选修课程超过2门且成绩都在80分以上的学生的学号。
select Sno
from SC
group by Sno
having COUNT(Cno)>2 and MIN(Greade)>80
(1)查询未选课的学生学号、姓名和所在系;
select Sno,Sname,Sdept
from Student
where Student.Sno NOT IN(
select Sno
from SC
);
(2)查询每个学生的选课情况包含学生学号、姓名、所选课程、成绩;
select Student.Sno,Sname,Cname,Greade
from Student,SC,Course
where Student.Sno=SC.Sno AND SC.Cno=Course.Cno
(3)查询没有学生选的课程信息;
select Cno,Cname,Cpno,Ccredit
from Course
where Course.Cno NOT IN(
select Cno
from SC
)
(4)查询每个学生选课的总学分数,并按总学分数的降序排列;
select Sno,Sname,Cname,Ccredit,Ccredit
from SC,Course,Student
where Sno is not null and SC.cno=Course.cno
group by sno
order by sum(Ccredit) desc
(5)查询选修了数学且成绩在80分以上的学生的学号、姓名、课程名及成绩;
select Student.Sno,Sname,Cname,Greade
from SC,Course,Student
where Student.Sno=SC.Sno and SC.Cno=Course.Cno and Cname='数学' and Greade>80
(6)查询选修数学并且成绩在85分以上的学生学号和姓名;
select Student.Sno,Sname
from SC,Course,Student
where Student.Sno=SC.Sno and SC.Cno=Course.Cno and Cname='数学' and Greade>85
(7)查找每个学生的选修课程情况(只查看选课的学生情况);
select Student.Sno,Sname,Course.Cno,Greade,Ccredit
from SC,Course,Student
where Student.Sno=SC.Sno and SC.Cno=Course.Cno
(8)查找所有学生的选课情况(若学生未选课也要包括在内);
select Student.Sno,Sname,Course.Cname,Greade,Ccredit
from student
left join SC
on Student.Sno=SC.Sno
left join Course
on SC.Cno=Course.Cno;