一、实验目的
1.掌握查询语句的语法格式与查询思想。
2.熟练掌握单表查询与集合查询。
3.重点掌握连接查询,理解嵌套查询的执行过程。
二、实验内容
1.在创建的s,p,j和spj表中完成以下查询:
(1)查询零件重量在10-20之间(包括10和20)的零件名和颜色。
select pname,color
from p
where weight between 10 and 20;
(2)查询所有零件的平均重量。
select avg(weight)
from p;
(3)查询供应商s3供应的零件信息。
select p.*
from spj,p
where spj.pno = p.pno and sno = 's3';
(4)查询各个供应商号及其供应了多少类零件。
select sno,count(distinct pno) pno_sum
from spj
group by sno
order by sno asc;
(5)查询供应了2类以上零件的供应商号。
select sno
from spj
group by sno
having count(distinct pno)>=2
order by sno asc;
(6)查询零件名以“螺”字开头的零件信息。
select *
from p
where pname like '螺%';
(7)查询给每个工程供应零件的供应商的个数。
select jno, count(distinct sno)sum_sno
from spj
group by jno
order by jno;
(8)查询供应总量在1000—2000之间(包括1000和2000)的零件名称。
select pname
from spj,p
where spj.pno = p.pno and qty between 1000 and 2000;
2.将实验一中创建的五张表用imp命令导入,在导入的表中完成以下查询:
(1)查询课程性质是选修,并且选修人数在60人以上的课程名、课程学时和开设学期。
select Cname,Chour
from Course
where Cproperty='选修' and cno in
(select cno
from sc
group by cno
having count(*)>60);
(2) 统计每个学院的学生人数。
select scollege,count(sno)
from student
group by scollege;
(3) 查询信息工程学院所有学生已修课程的总学分,要求列出学号、姓名和总学分。
select student.sno,sname,sum(credit)
from student,sc,course
where student.sno = sc.sno and sc.cno =course.cno
and grade>60
group by student.sno,sname;
(4) 查询吴春燕老师所授课程的选课和成绩信息,要求列出该老师所授课程的课程名,选课的学生姓名和课程成绩。
select Cname,Sname,grade
from SC,Teacher,Student,TC,course
where sc.cno = course.cno and student.sno =sc.sno and tc.tno =teacher.tno
and tc.cno = course.cno and Tname='吴春燕';
(5) 查询同时选修了“中间件技术”和“Java EE技术”两门选修课的学生的姓名。
select Sname
from Student,sc,course
where student.sno = sc.sno and sc.cno = course.cno and cname='Java EE技术'
intersect
select Sname
from Student,sc,course
where student.sno = sc.sno and sc.cno = course.cno and cname='中间件技术';
(6) 查询1994年1月1日以前出生的学生的姓名和专业。
select sname,smajor
from student
where to_char(sbirth, 'yyyy')<'1994';
(7) 查询选修了5门以上课程的学生学号和姓名。
select sno,sname
from student
where student.sno in
(select sno
from sc
group by sno
having count(*)>5);
(8) 查询比本院学生平均年龄小的学生信息,要求列出姓名与年龄。
select sname,2022-to_char(sbirth,'yyyy') sage
from student s1
where 2022-to_char(sbirth,'yyyy')<
(select avg(2022-to_char(sbirth,'yyyy'))
from student s2
where s2.scollege= s1.scollege);
(9)查询一门课也没有带的教师姓名。
select tname
from teacher
where not exists
(select *
from tc
group by tno
having count(cno)>0);
(10)查询比所有“计算机科学与技术”专业学生年龄都大的学生。
select *
from Student
where 2022-to_char(sbirth,'yyyy')>all
(select 2022-to_char(sbirth,'yyyy')from student
where smajor = '计算机科学与技术');
三、实验总结
1.通过上机操作,熟练掌握了查询的相关语句,熟悉了常用的查询条件,掌握了集函数的相关用法。
2.对于连接查询和嵌套查询的使用可以灵活掌握。
3.实验中对于复杂查询的设计欠缺,不太灵活。有通过在网络上查阅了一些博主的写法,体会其设计思想,加强合理编写查询语句的能力。