实验四 数据查询实验

use  StudentDB ;

/**********************实验四 数据查询实验******************************/
-- 1.   查询学生的基本信息;
select *from Student;
-- 2.   查询“CS”系学生的基本信息;
select *from Student where Sdept='CS';
-- 3.   查询“CS”系学生年龄不在19到21之间的学生的学号、姓名;
select Sno,Sname from Student
where Sdept='CS' and Sage not between 19 and 21 ;
-- 4.   找出最大年龄;
select max(Sage) 最大年龄 from Student ;
-- 5.   找出“CS”系年龄最大的学生,显示其学号、姓名;
select Sno,Sname from Student
where Sdept='CS' and Sage= (
    select max(Sage) from Student where sdept='CS');
-- 6.   找出各系年龄最大的学生,显示其学号、姓名;************
-- 本题用到了相关子查询,比较的难,需要多加练习
update  student set sage=23 where sno='200215125' ;
select *from student;
select sno,sname,sdept
from student a
where sage = (
    select max(sage) from student b
    where a.sdept=b.sdept );
-- 7.   统计“CS”系学生的人数;
select count(*) 计算机系人数 from Student
where Sdept='CS';
-- 8.   统计各系学生的人数,结果按升序排列;
select Sdept,count(*) cnt
from SC,Student
where Student.Sno=SC.Sno 
group by Sdept
order by cnt;
-- 9.   按系统计各系学生的平均年龄,结果按降序排列;
select Sdept,avg(Sage) age
from SC,Student
where Student.Sno=SC.Sno
group by Sdept
order by age desc;
-- 10.  查询每门课程的课程名;
select Cname from Course;
-- 11.  查询无先修课的课程的课程名和学分数;
select Cname,Ccredit
from Course
where Cpno is null;
-- 12.  统计无先修课的课程的学分总数;
select sum(Ccredit) 
from Course
where Cpno is null ;
-- 13.  统计每位学生选修课程的门数、学分及其平均成绩;
select SC.sno,count(SC.Cno),sum(Ccredit),avg(Grade)
from SC,Course
where SC.cno=Course.cno
group by Sno;
-- 14.  统计选修每门课程的学生人数及各门课程的平均成绩;
select Cno,count(Cno),avg(Grade)
from SC
group by Cno;
-- 15.  找出平均成绩在85分以上的学生,结果按系分组,并按平均成绩的升序排列;
select Student.Sno,avg(grade) avg_grade,Sdept
from SC,Student
where SC.Sno=Student.Sno
group by Student.Sno,Sdept
having avg(grade)>85
order by avg_grade;
select Student.Sno,avg(grade) avg_grade,Sdept
from SC join Student
using(sno)
group by Student.Sno,Sdept
having avg_grade>85
order by avg(grade) ; -- 注意在order by 后面可以用聚集函数
-- 16.  查询选修了“1”或“2”号课程的学生学号和姓名;*********
-- 方法一
select distinct Student.Sno,Sname
from Student,SC
where SC.Sno=Student.Sno and (Cno='1' or Cno='2' );
-- 方法二
select distinct Student.Sno,Sname
from Student,SC
where SC.Sno=Student.Sno and Cno in('1','2');
-- 方法三
select distinct Student.Sno,Sname
from Student,SC
where SC.Sno=Student.Sno and Cno='1'
union
select distinct Student.Sno,Sname
from Student,SC
where SC.Sno=Student.Sno and Cno='2' ;
-- 17.  查询选修了“1”和“2”号课程的学生学号和姓名;*********
select distinct Student.Sno,Sname
from Student,SC
where SC.Sno=Student.Sno and  Cno='1' and Student.Sno in
    (select  distinct Sno from SC where  Cno='2' );
/*
-- intersect 在MySQL中没有
select distinct Student.Sno,Sname
from Student,SC
where SC.Sno=Student.Sno and Cno='1'
intersect
select distinct Student.Sno,Sname
from Student,SC
where SC.Sno=Student.Sno and Cno='2' ;
*/
-- 18.  查询选修了课程名为“数据库”且成绩在60分以下的学生的学号、姓名和成绩;
insert into SC values('200215125','1',58);
select Student.Sno,Sname,Grade
from SC,Course,Student
where SC.Sno=Student.Sno and SC.Cno=Course.Cno and Cname='数据库' and grade<60 ;
delete from SC 
where Sno='200215125' and Cno='1' ;
-- 19.  查询每位学生选修了课程的学生信息(显示:学号,姓名,课程号,课程名,成绩);
select Student.Sno,Sname,Course.Cno,Cname,grade
from Student,Course,SC
where Student.Sno=SC.Sno and Course.Cno=SC.Cno;
select sno,sname,cno,cname,grade
from student join sc using(sno)
join course using(cno);
-- 20.  查询没有选修课程的学生的基本信息;*************
-- 方法一
select Student.* from Student
where Sno not in (
    select Sno from SC );
-- 方法二
select Student.* from student
where not exists (
    select * from SC
    where sno=Student.sno );
-- 21.  查询选修了3门以上课程的学生学号;
select Sno from SC
group by Sno
having count(Sno)> 2;
-- 22.  查询选修课程成绩至少有一门在85分以上的学生学号;
select Sno from SC
group by Sno
having max(Grade)>85 ;
-- 23.  查询选修课程成绩均在85分以上的学生学号;
select Sno from SC
group by Sno
having min(Grade)>85 ;
-- 24.  查询选修课程平均成绩在85分以上的学生学号;
select Sno from SC
group by Sno
having avg(Grade)>85 ;



数据库概念实验代码。。 在S,C,SC上完成以下查询: 1. 查询学生的基本信息; 2. 查询“CS”系学生的基本信息; 3. 查询“CS”系学生年龄不在19到21之间的学生的学号、姓名; 4. 找出“CS”系年龄最大的学生,显示其学号、姓名; 5. 找出各系年龄最大的学生,显示其学号、姓名; 6. 统计“CS”系学生的人数; 7. 统计各系学生的人数,果按升序排列; 8. 按系统计各系学生的平均年龄,果按降序排列; 9. 查询无先修课的课程课程名和学时数; 10.统计每位学生选修课程的门数、学及其平均成绩; 11. 统计选修每门课程学生人数及各门课程的平均成绩; 12. 找出平均成绩在85以上的学生果按系组,并按平均成绩的升序排列; 13. 查询选修了“1”或“2”号课程学生学号和姓名; 14. 查询选修了“1”和“2”号课程学生学号和姓名; 15. 查询选修了课程名为“数据库系统”且成绩在60以下的学生的学号、姓名和成绩; 16. 查询每位学生选修了课程学生信息(显示:学号,姓名,课程号,课程名,成绩); 17. 查询没有选修课程学生的基本信息; 18. 查询选修了3门以上课程学生学号; 19. 查询选修课程成绩至少有一门在80以上的学生学号; 20. 查询选修课程成绩均在80以上的学生学号; 21. 查询选修课程平均成绩在80以上的学生学号;
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值