数据库原理上机实验2(数据基本查询)
·
use xhy; #使用数据库
#(1)单表查询(实现投影操作)
#查询全体学生的学号、姓名、所在系
select *
from student;
#(2)单表查询(实现选择操作) where
#查询所有年龄在20到23岁之间(包含)的学生姓名、年龄.
select sname,sage
from student
where sage>=20 and sage<=23;
#(3)不带分组过滤条件的分组统计查询
#查询各个课程号及相应的选课人数
select cno,count(sno)
from course
group by cno;
#(4)带分组过滤条件的分组统计查询
#查询选修了三门以上课程的学生学号
select sno,count(cno)
from course
group by sno
having count(cno)>=3;
#查询平均成绩大于等于80分的学生学号、平均成绩
select sno,avg(grade)
from course
group by sno
having avg(grade)>=80;
#(5)单表自身连接
#查询每一门课的间接先修课(即先修课的先修课)
select a.cno cno,a.cname cname,a.cpno cpno,b.cpno cpnocpno
from cs a,cs b
where a.cpno=b.cno
order by a.cno;
#(6)两表连接查询(普通连接)
#查询每个学生及其选修课程的情况
select xs.*,kc.*
from student xs,course kc
where xs.sno=kc.sno;
#(7) 两表连接查询(自然连接)
#查询每个学生及选修课程情况,去掉重复列
select xs.sno,sname,ssex,sage,sdept,cno,kc.grade
from student xs,course kc
where xs.sno=kc.sno;
#(8) 三表连接查询
#查询每个学生的学号、姓名、选修的课程名、成绩
select student.sno,sname,ssex,sage,sdept,course.cno,cname,grade,ccredit
from student,course,cs
where student.sno=course.sno and course.cno=cs.cno;
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐


所有评论(0)