本文在实验四的基础上增加了orderby、聚集函数、groupby、多表查询的知识点,相较于上一次实验的难度变大了,嵌套表达更多了,逐渐开始套娃……
其实可以看成一个偏正短语来拆分,再写成SQL语句,比如查询一个年龄大于20的学生成绩,那么学生成绩就为中心语,年龄大于20则作为修饰语,将中心语放至select ...from
之间,依次类推进行分析。
- 中心语:Student.sno,sname,sdept,Course.cno,SC.grade放到前面
- 由于所查询的内容涉及三个表,所以将三表按照索引进行合并Student.sno=SC.sno and Course.cno=SC.cno
- 修饰语:课程为‘C language’或者‘Database’=》为在课程名字中查询‘C language’或者‘Database’
select Student.sno,sname,sdept,Course.cno,SC.grade
from Student,Course,SC
where Student.sno=SC.sno and Course.cno=SC.cno
and Course.cname in ('C language','Database');
- 中心语:学生的详细情况Student.*
- 合并Student,SC两表Student.sno=SC.sno
- 修饰语:成绩缺少grade is null
select Student.*
from Student,SC
where Student.sno=SC.sno and grade is null;
- 中心语:学生信息Student.*
- 从表Student提取信息
- 修饰语:与‘Lisi’年龄不同;提取年龄为中心语,Lisi的年龄为修饰语
select Student.*
from Student
where sage not in (select sage from Student where sname='Lisi');
- 中心语:学生学号、姓名及平均成绩Student.sno,sname,avg(SC.grade)
- 合并Student,SC两表Student.sno=SC.sno
- 修饰语:课程的平均成绩avg(Grade)
- 子语句:‘Lisi’的平均成绩=》查李四的成绩,嵌套一个子语句:select avg(grade)from SC,Student where sname='lisi’and Student.sno=SC.sno
select Student.sno,sname,avg(grade)
from Student ,SC
where Student.sno=SC.sno
group by Student.sno,sname
having avg(Grade)>(select avg(grade)from SC,Student where sname='lisi'and Student.sno=SC.sno)
由于条件语句中有聚集函数avg所有使用group by … having
- 中心语:学号,姓名,所在院系,已修学分
- 三表按照索引进行合并Student.sno=SC.sno and Course.cno=SC.cno
- 修饰语:已经及格的课程学分之和
select Student.sno,sname,sdept,sum(ccredit)
from Student,SC,Course
where Student.sno=SC.sno and SC.cno=Course.cno
and grade>60 group by student.sno,sname,sdept;
- 中心语:学生的学号、姓名、院系及成绩
- 涉及两个表内容,连接两表:Student.sno=SC.sno
- 修饰语:只选修一门的课程
- 子语句中心语:课程
- 子语句修饰语:只选修一门
select Student.sno,sname,sdept,grade
from Student,SC
where Student.sno=SC.sno
and SC.sno in (select sno from SC group by SC.sno having count(distinct cno)=1);
- 中心语:学生的学号、姓名及课程号
- 二表按照索引进行合并Student.sno=SC.sno
- 修饰语:选修了至少一门和‘Lisi’选修课程一样
- 子语句中心语:课程一样的学生
- 子语句修饰语:‘Lisi’选修的课程
select Student.sno,sname,cno
from Student,SC
where Student.sno=SC.sno
and SC.sno in
(select SC.sno from SC,Student where cno in
(select cno from Student,SC where sname='lisi' and Student.sno=SC.sno));
- 中心语:学生的基本信息
- 三表按照索引进行合并Student.sno=SC.sno and Course.cno=SC.cno
- 修饰语:至少选修’C language’或’Database’课程
- 子语句中心语:课程
- 子语句修饰语:选修’C language’或’Database’
select Student.sno,sname,sdept,SC.cno,cname,grade
from Student,SC,Course
where Student.sno=SC.sno and Course.cno=SC.cno
and SC.cno in
(select SC.cno from SC,Course where (cname='C language' or cname='Database') and SC.cno=Course.cno);
- 中心语:学生的基本信息
- 三表按照索引进行合并Student.sno=SC.sno and Course.cno=SC.cno
- 修饰语:至少选修’C language’或’Database’课程
- 子语句中心语:课程
- 子语句修饰语:选修’C language’和’Database’
select Student.sno,sname,sdept,SC.cno,grade
from Student,SC,Course
where Student.sno=SC.sno and Course.cno=SC.cno
and SC.sno in (select SC.sno from SC,Course where (cname='C language' or cname='Database')
and SC.cno=Course.cno group by SC.sno having count(SC.cno)>=2);
- 中心语:课程号、课程名、学号、姓名及成绩
- 三表按照索引进行合并Student.sno=SC.sno and Course.cno=SC.cno
- 修饰语:课程被选修
select Course.cno,cname,Student.sno,sname,grade
from Student,SC,Course
where Student.sno=SC.sno and SC.cno=Course.cno
order by Course.cno;
- 中心语:课程号、课程名
- 二表按照索引进行合并Course.cno=SC.cno
- 修饰语:只被一名学生选修的课程
select SC.cno,cname
from SC,Course where Course.cno=SC.cno
group by SC.cno,cname having count(SC.cno)=1;
11.使用嵌套查询列出选修了’Data structure’课程的学生学号和姓名
- 中心语:学生学号和姓名
- 三表按照索引进行合并Student.sno=SC.sno and Course.cno=SC.cno
- 修饰语:选修了’Data structure’课程
- 子语句中心语:课程
- 子语句修饰语:选修了’Data structure’
select Student.sno,sname
from Student,SC,Course
where Student.sno=SC.sno and Course.cno=SC.cno
and SC.cno in
(select SC.cno from SC,Course where SC.cno=Course.cno and Course.cname='Data structure');
- 中心语:学生姓名、年龄和院系
- 从表Student选取
- 修饰语1:其它系
- 修饰语2:年龄小于AI的某个学生的年龄
select sname,sage,sdept
from Student
where sage<any (select sage from Student where sdept='AI') and sdept<>'AI';
https://blog.csdn.net/qq_43776450/article/details/95965866
https://blog.csdn.net/qq_43717736/article/details/107722502
https://max.book118.com/html/2017/0326/97190702.shtm